March 20, 2007

Not so Random Numbers

Category: UNIX Scripting — Raffael Marty @ 9:05 pm

In cryptography or science in general, you often need perfect random numbers. Well, up to today, that was my need as well. However, today I was trying to generate numbers that are not too random, but have a certain bias. I think it’s kind of ironic. Googling for a solution is almost impossible. Every link shows a perfect random number generator 😉

I don’t care what the bias is in the numbers that are generated. Actually, the bias can be pretty high. Anyone have a method to do this in Perl?

Can you do something like int(rand($upperLimit*1000)) % 1000 ??? Basically changing the interval from where the random number is taken and then shrinking it again?

5 Comments »

  1. I guess I am going to answer my own question. I believe this is doing the trick:
    int(rand($upperLimit)^2)]
    You square your random number and you generate a bias towards 1. If you cube, the bias will be worse, etc. Yes?

    Comment by Raffy — March 20, 2007 @ 9:31 pm

  2. Wow, my Perl skills are certainly not the best. The idea was right, but here goes the correct way:
    int((rand()**2)*$upperLimit)

    Comment by Raffy — March 20, 2007 @ 9:42 pm

  3. What, you want to take a flat line between 0 and 1, and slant the right a up bit? Take a random number and add a second random number divided by 10 or 100.

    Comment by Ryan Russell — March 23, 2007 @ 11:19 pm

  4. Oh, wait. I’m trying to reinvent the formula for slope. Yeah, just multiple by 1.1, square it, etc…

    Comment by Ryan Russell — March 23, 2007 @ 11:21 pm

  5. It depends how much selectivity you want over the bias. I believe if you want total selectivity over the bias in the frequency domain you will have to apply an inverse FFT to a good, linear congruential generator.

    It would probably be a good idea to read about white and pink noise distributions.

    Comment by eris — September 21, 2011 @ 12:33 pm

RSS feed for comments on this post. | TrackBack URI

Leave a comment

XHTML ( You can use these tags): <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> .