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?

## All 7 Replies

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.

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.

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;
}``````

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.

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..

commented: Yes. you're right. Rand() is probably broken. :icon_rolleyes: -2
commented: Fail. -4

try n=(rand() % 9000)*(rand() % 6) 6 being a multiplication of the 0-(whatever your max is) that will represent the max value.

commented: This thread is over 7 years old -2

You can try to write a main function and generate a random array.

``````#include<iostream>
#include<cstdlib>
#include<ctime>
#define N 50000
void createArray(int array[])
{
srand((unsigned int)time(NULL));
for (int i = 0; i < len; i++)
{
int num = (int)((double)rand()/RAND_MAX*N);//N can be any maximum you want.
array[i] = num;
}
}``````
commented: what +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.