Randomization

Discussion in 'Computer Science & Culture' started by SpyMoose, Jul 30, 2003.

Thread Status:
Not open for further replies.
  1. SpyMoose Secret double agent deer Registered Senior Member

    Messages:
    1,641
    How exactly does a computer generate a random number? I thought it would become apparent to me once i learned some programing, well im in my first programming class ever, learning VB.net and all you do basicly is type "Random" and a small equation that has more to do with an array you have built than anything else.

    When asked, my Prof. said the computer gets a number (which he said was the # of miliseconds passed midnight) from the system clock, then preforms some calculation on it and thats what gets you your random number. I found that odd because that would mean that at the same times of day you would alwayse get the same "random" results. But then again he didnt seem to sure about his explaination, so anyone know how its done?
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. Voodoo Child Registered Senior Member

    Messages:
    1,296
    There are a few ways depending on the exact definition of randomness.

    1) Measure something random like radioactive decay.

    2) Measure something, that for all intents and purposes is random. eg. noise from a microphone, the time between keypresses etc.

    3) Use an algorithm that generates pseudo-random number. eg. a linear congruential algorithm, which is of the form

    x<sub>n+1</sub> = (ax<sub>n</sub> + b) mod m

    where x is the seed value. The algorithm gives you a new x that you plug into the equation again.
    If you choose the seed value and the multiplier wisely you get about 3 million values before they start to repeat themselves, until then there are no discernable patterns*. Good values are m=2<sup>32</sup>, a = 69069, b =1, x = 0. The advantage of this system is that the numbers are random enough for most purposes, and you can easily recreate them.

    What it sounds like your guy is doing is using the milliseconds since midnight as a seed value for a LC formula(or other eg. additive congruential or multiplicative congruential, or something else entirely)

    *actually there are, but they are rather insignificant.
     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. malkiri Registered Senior Member

    Messages:
    198
    An example of #2 is LavaRnd.
    The C standard library's rand() function is really only useful in applications where you don't really care too much how random the numbers are. The distribution isn't uniform - that is, you have different chances of getting different numbers. This means if you were to find its modulo 4 of several numbers produced by rand(), the number of times you'll get the values 0 - 3 likely won't be equal, even though statistically they should be. I hear Java's RNG is fairly good.
    There are a number of good replacement RNGs out there. One is the Mersenne Twister.
    If I had to guess, I would hope that your professor actually means the number of seconds passed since the epoch, which is 12:00 AM Jan. 1, 1970. If he did mean milliseconds since midnight, you're right - if the RNG is seeded with the same time each day, you'll get the same 'random' numbers.
    By the way, it's more correct to refer to them as 'pseudo-random numbers,' since they truly are not random.

    Please Register or Log in to view the hidden image!



    "Anyone who attempts to generate random numbers by deterministic means is, of course, living in a state of sin." - John von Neumann

    Edit: Reread the modulo part and realized it didn't make sense.

    Please Register or Log in to view the hidden image!

     
    Last edited: Jul 31, 2003
  6. Google AdSense Guest Advertisement



    to hide all adverts.
  7. SpyMoose Secret double agent deer Registered Senior Member

    Messages:
    1,641
    That VonNeumen quote is very funny.

    I was just curious if there was a standard way that most of the computer world had decided was pretty much the best way to get a random number out of a PC when they wanted one, you guys seem to be saying techniques differ.
     
  8. Christian Sodomy Registered Senior Member

    Messages:
    329
    Depends on how random the number needs to be. If I only need to pick a number between 1 and 5, it's a different story from needing a stream of numbers with varied values to use in, say, a character "AI" in a videogame.
     
  9. disposable88 My real name is Rick Registered Senior Member

    Messages:
    76
    At AT&T, when my father was testing random numbers with the 5ESS board, he took the degree temperature outside down to the thousandths place or so.

    That's how he got random numbers, but milliseconds after midnight is also a pretty popular one at AT&T and Bell Labs.
     
Thread Status:
Not open for further replies.

Share This Page