Thread: rand();

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    14

    rand();

    well im triing to get random numbers between say 23 and 35. if i use rand()%35 than it picks numbers between 0 and 35. what can i do.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Get a random number between 0 and 12, then add 23. Simple.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >well im triing to get random numbers between say 23 and 35.
    r = ( rand() * ( 35 - 23 ) ) / RAND_MAX + 23;

    >if i use rand()%35 than it picks numbers between 0 and 35.
    Small ranges like that are frighteningly not random, so you'd best stick with the code I gave above. It uses the high order bits of a value instead of the low order bits which your method uses.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Originally posted by Prelude
    >well im triing to get random numbers between say 23 and 35.
    r = ( rand() * ( 35 - 23 ) ) / RAND_MAX + 23;

    >if i use rand()%35 than it picks numbers between 0 and 35.
    Small ranges like that are frighteningly not random, so you'd best stick with the code I gave above. It uses the high order bits of a value instead of the low order bits which your method uses.

    -Prelude
    Assuming that rand() generates a random number uniformly among the integers 0 to RAND_MAX , your function will not draw numbers uniformly from 23,24,...35 . The reason being that r can be 35 if and only if rand() is RAND_MAX , the probablilty of that (assuming ideal behavior of rand()) is 1/(RAND_MAX+1) <=1/32768 ( the minimum permitted value of RAND_MAX is 32767 ) way less than 1/(35-23+1)=1/13
    Last edited by pinko_liberal; 07-08-2002 at 11:41 PM.
    The one who says it cannot be done should never interrupt the one who is doing it.

  5. #5
    Registered User dune911's Avatar
    Join Date
    Sep 2001
    Posts
    140
    i use the following function in my program :
    Code:
    int my_rand(int max)
    {
        return 1+rand()%(max);
    }
    now call my_rand(20); to get a random number between 1 and 20.
    replace the return with "return 23+rand()%(max)" and call the
    function with my_rand(35); so you'll have your random number

    remember to use
    Code:
    srand(time(NULL));
    in your program to get real random numbers

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >...in your program to get real random numbers
    There's no such thing
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User dune911's Avatar
    Join Date
    Sep 2001
    Posts
    140
    @Hammer
    >> There's no such thing
    what do you mean?

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    what do you mean?
    How do you think the rand function works? Numbers obtained through rand() are psuedorandom in nature and not truly random. There is a mathematical equation used to generate these numbers and not some little fairie that picks a number out of a hat when you call the function. An initial value called the seed, which can and should be reset using the srand function, is used along with the algorithm to generate a psuedorandom sequence of values via the rand function. If the same seed value is used, either through just using the default value or setting it to the same value each time your program starts, then you will see that you get the same set of "random" values every single time.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Unregistered
    Guest

    ahhh...

    no need to get very complex...

    int mynumber = 23+rand()%2;

    but i dont know if this is more effective than the rest..

    rand()%x gives you a number between 0 and x

    23 + rand()%x gives you a number between
    0+23 and x+23 ... in this case, 23 and 25

    COOL PROGRAMS @ www.akilla.tk

  10. #10
    Registered User dune911's Avatar
    Join Date
    Sep 2001
    Posts
    140
    well... that's what i thought thanks

  11. #11
    Registered User
    Join Date
    May 2002
    Posts
    66
    A PC can never generate true random numbers without additional devices (such as environmental samplers, timed user input, etc). RNG (random number generator) is an algorithm and very predictable without an input which cannot be predicted (such as nanosecond delay between human key strokes, current ambient temperature inside the computer case at the time of sampling, etc).

  12. #12
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    What are true random numbers , how do you define true random numbers ? If I give you a sequence of 100,000 numbers between 0 and 1 , will you be able to say whether they are truly random or have been genrated by a RNG , if you plan to use a battery of statistical tests most of the good RNG's pass almost all of the statistical tests so you must have some other criterion .
    The one who says it cannot be done should never interrupt the one who is doing it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rand() implementation
    By habert79 in forum C Programming
    Replies: 4
    Last Post: 02-07-2009, 01:18 PM
  2. Wm_timer
    By Ducky in forum Windows Programming
    Replies: 21
    Last Post: 09-26-2008, 05:36 AM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. rand() to choose?
    By wagman in forum C++ Programming
    Replies: 2
    Last Post: 03-27-2002, 01:43 AM
  5. rand () a little confusion
    By Led Zeppelin in forum C Programming
    Replies: 3
    Last Post: 03-19-2002, 10:13 PM