Thread: random isn't really random?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    43
    I got the same result
    H T T T T H H H H
    T T T H H H H H H
    ......

    and the same count for each H and T for every execution...
    perhaps, I should use "srand"

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by azsquall View Post
    I got the same result
    H T T T T H H H H
    T T T H H H H H H
    ......

    and the same count for each H and T for every execution...
    perhaps, I should use "srand"
    Yes, if you want each time you run the code to produce different results, you should use some sort of random seed that varies from time to time.

    All of the traditional random number generators that are used by computers generate a series of numbers based on a "seed". If you start with the same seed, you get the same sequence. If you start with very similar seeds, it's likely that you get a similar result - but by no means guaranteed.

    So running your code twice in a row using time(0) as a seed may produce a very similar result, because the "time" may be close enough to produce the same result. If you wait for several seconds before running it again, then you would be more safe.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    81
    heh. welcome to the world of PRNGs.

    QRBG is the way to go http://cboard.cprogramming.com/showthread.php?t=103071

    kidding . srand(time(NULL)) would probably work ok for you, though I found that one needs to wait at least 1 second between each call, and even then the results were largely linear.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random to int?
    By psyadam in forum C# Programming
    Replies: 7
    Last Post: 07-22-2008, 08:09 PM
  2. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 08:16 AM
  3. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  4. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM