#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    3
    Rep Power
    0

    Pseudo-random Generator in C


    Hi. I am a fairly new C programmer (I'm more experienced in Java and C++) and was just wondering if the C libraries provided a way to generate a random integer? Thank you!
    Jill
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    190
    int rand()

    You should defintively get a C reference book...
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    3
    Rep Power
    0
    Thank you very much for your help.
  6. #4
  7. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    ... and a C++ reference book by the look of it ;-)
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    138
    Rep Power
    11
    You could also look into srand if you want to create different random number each time. rand generates the same random number each time it is called
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    190
    ... a very useful "feature" in debugging ;)
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    138
    Rep Power
    11

    Smile


    Absolutely true, but was mentioning that srand could be used too to create random numbers:) ;)
  14. #8
  15. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    It may be that you understand what you mean but expressed it poorly, but your two posts could be considered misleading.

    srand() doesn't generate random numbers, it seeds the random number generator. rand() doesn't generate the same number each time it's called, it returns the next number in the sequence. If you don't seed the generator before your first call to rand() it may start the sequence from the same point each time.
  16. #9
  17. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243

    What are you using the random values for?


    rand() is a very poor random number generator, useless for almost everything except for student exercises. If you are doing simulations (or, heaven forbid, cryptography) you will find that rand() is going to give anomalous results. In addition, it only provides a small set (it only provides numbers between 0 and 32767 and if I recall correctly, providing a different seed to srand() only shifts your starting point in the exact same sequence of random numbers.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  18. #10
  19. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12

    Re: What are you using the random values for?


    Originally posted by mitakeet
    rand() is a very poor random number generator, useless for almost everything except for student exercises. If you are doing simulations (or, heaven forbid, cryptography) you will find that rand() is going to give anomalous results. In addition, it only provides a small set (it only provides numbers between 0 and 32767 and if I recall correctly, providing a different seed to srand() only shifts your starting point in the exact same sequence of random numbers.
    It provides at least 0 to 32767. Some implementations provide a greater range.
  20. #11
  21. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    The RAND_MAX macro is set to equal to the maximum range rand() returns. It has been 32,767 on every machine I have tested it on (all Windows OS's). srand() does simply shift the starting point in the sequence, since rand() just returns the next number in the sequence based on the current number, the sequence is always the same.

    You could attempt to use the least significant bits of the current time via high resolution timer, if one exists, to help construct a random number. These bits from the current time should be fairly random by themselves.
  22. #12
  23. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    3
    Rep Power
    0
    The random generator isn't for anything too fancy. I just have to write a small program in C that implements MPI on our computer cluster. I was just trying to figure out how to make calls to random processes. Thank you all for your help! I think I am all set now.
  24. #13
  25. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    Originally posted by Jason Doucette
    The RAND_MAX macro is set to equal to the maximum range rand() returns. It has been 32,767 on every machine I have tested it on (all Windows OS's).
    That wouldn't surprise me, and is consistent with what I said. But there are platforms using versions of RAND_MAX with much higher ranges, e.g. 0 .. 2,147,483,647.

IMN logo majestic logo threadwatch logo seochat tools logo