954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

RAND_MAX: how do i set?

i have made a program in school that chooses a random number and you need to guess it. you put in a number and it says higher or lower.
the only problem is that i can't set RAND_MAX and its a 4 or 5 diget number (havent figured it out yet). the help explains RAND_MAX but doesn't say how to set it.
plz help
thnx
a confused yr 9
humbug

P.S.do you need to refresh the page to see replys?

humbug
Junior Poster in Training
93 posts since Oct 2005
Reputation Points: 20
Solved Threads: 13
 

You don't set RAND_MAX, it's a constant value (usually 32767). I don't see why you would want to set it unless the range is too small for you (in which case you use a better random number generator). Tell us how you're trying to go about solving the problem, show us your code, and we'll help you with it.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

thanks

the code is at school and i cant remember exactly how its writen so ill get it from school later. (probably tomorow or the next day)

im using srand((????)time(NULL)) (coppied from the example).

byt the way, why douse RAND_MAX exist if it cant be set? is there another reason?

i saw the other way of making a random nunber but it looked cofusing so stuck with the other method.

humbug
Junior Poster in Training
93 posts since Oct 2005
Reputation Points: 20
Solved Threads: 13
 

The reason RAND_MAX exists is for the maximum digit the rand() function can return. If you want a quick and dirty random number just use the modulus.
Like this:

#include <stdlib.h>
/*Or <cstdlib> for C++*/
int main()
{
    /*Will return a number between 0 - 9*/
    int number = rand() % 10;
    return 0;
}
prog-bman
Junior Poster
109 posts since Nov 2004
Reputation Points: 14
Solved Threads: 4
 
byt the way, why douse RAND_MAX exist if it cant be set? is there another reason?


RAND_MAX doesn't 'exist', for some definitions of the word. It just happens to be a fact that rand() will return a value that is less than or equal to whatever value RAND_MAX denotes. It's not a variable in memory; it gets replaced with a regular old number by the compiler. For example, my compiler replaces it with the value 2147483647. The value you get depends on the random number generator your version of rand() uses.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

rand() is awsome... i love using it in my codes... however, i have had times when rand() has returned garbage, especially when you are trying to calculate third order differential eqns using monte carlo..

mcodesmart
Newbie Poster
17 posts since Mar 2010
Reputation Points: 4
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You