Thread: random number between 0 and n??

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    53

    random number between 0 and n??

    okay, I know that the following will give me a number between 0 and 100; however, if I wanted to go from 0 to a number n, how would i do that ? Perhaps 44 or any number ?? Thanks

    Code:
    srandom(time(0));
    win=random()%100;

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    Same thing, but replace the literal with a variable :-)
    Code:
    srandom(time(0));
    win=random()%n;
    *Cela*

  3. #3
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Btw: I'm pretty sure win=random()%100; would give you a number between 0 & 99, not 0 & 100. So in your function you may want to use (n+1) as the variable.

    ie
    Code:
    int n=44;
    srandom(time(0));
    win=random()%(n+1); // Gives a random number between 0 & n (inclusive)
    Demonographic rhinology is not the only possible outcome, but why take the chance

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    53

    thanks...

    Thank you.

Popular pages Recent additions subscribe to a feed