Random random seeds

This is a discussion on Random random seeds within the C++ Programming forums, part of the General Programming Boards category; Code: srand(time(NULL)); srand(rand() % 5000); Would that be more random than just calling the time seed alone?...

  1. #1
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552

    Random random seeds

    Code:
      srand(time(NULL));
      srand(rand() % 5000);
    Would that be more random than just calling the time seed alone?

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    No not really. You will only seed srand with a different number.

  3. #3
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    I've ran some tests on both. My method seems to be more random. I tested with really small numbers (0-5) small numbers (0-50) and large numbers (0-500)

    Seeding with just time had some repeating numbers, my method had the numbers more spread apart.

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    But that doesn't nececsary mean that the numbers are more random. The series
    '1,1,1,1,1' is just as likely as '2,50,765,78,912'

  5. #5
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    well is there ANY way to make the random numbers more random?

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    48
    random is random... I've never had a problem with random numbers b4 where i get the same number each time. I use the random seed generator:
    Code:
    srand(GetTickCount())
    with <windows.h> and that always works fine for me.
    Hey, you gotta start somewhere

  7. #7
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    BASIC's system, once compiled, would always select the same numbers. Its just that you wouldn't have to specify the numbers. Theoretically, using that code shouldn't make much of a difference, but go ahead and try it. Once something is random, it's about as random as it's going to get. It's like if I shoot something, it's dead. I can't shoot it again to make it more dead.

  8. #8
    Unregistered
    Guest
    Wouldn't frenchfry's way actually be alot less random? Using the time as a seed will always give you a different seed than before, as time goes forward all the time. But with his, you'd only have 5000 (or whatever number he chooses) different seeds, and thus, it would give out the same numbers every 5000 times you run the program, or so. Ofcourse it might choose the same number more often, but you know what I mean.

  9. #9
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    no, because I already seeded it with time.

    wait, your right!

  10. #10
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    rand() is a psuedo-random number generator. It will always produce the same sequence of "random" numbers with a given seed.
    srand() take an unsigned int as its argument so the total possible number of seeds is ~65500 for a 2-byte int and ~4,300,000,000 for a 4-byte int (which is more likely now days). Which means, it is possible to have the same seed every now and again but rather unlikely. As pointed out, if you mod by 5000, you have just cut down the total possible seeds to only 5000.
    If a tree falls in the forest, and no one is around to see it, do the other trees make fun of it?

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Also, you should use RAND_MAX to calculate your random numbers:
    (rand() * 5) / RAND_MAX

    See this thread for an explanation:
    http://www.cprogramming.com/cboard/s...highlight=rand

  12. #12
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    French, in most cases srand() and rand() provide sufficently random number for an application.

    Also, if you WERE to come up with a completely random random number generator, you would most likely get a PhD out of it. The security and encryption industry is always looking for more random random numbers.

  13. #13
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    how about this (I'm just trying to bug you with this one )

    Code:
    srand(time(NULL));
    srand(rand() % time(NULL));

  14. #14
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    french, I can tell you very simply why that would fail to produce a more random number.

    The only unique number given to srand is time(). Therefore, there is an upper limit on randomness set at srand(unsigned(time(NULL)))

    Think about it this way:

    For any given value returned by time(NULL), or any given set of values returned by multiple calls to time(NULL), the pseudo-random number generator can only be seeded in one manner. Subsequent calls to rand() will return numbers based only on this value or set of values.

    If anyone were to write an algorithm that returned truly random numbers, it would not take a seed value. But this is beyond the scope of modern computer science. This is how random numbers tie in with encryption. Due to the ones and zeros nature of a computer, any random pattern of numbers is simply an ordered pattern of numbers whose order has been obfusciated beyond the casual appearance of a pattern.

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, 09:09 PM
  2. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 09: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, 03:40 PM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 05:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21