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.
This is a discussion on rand(); within the C Programming forums, part of the General Programming Boards category; well im triing to get random numbers between say 23 and 35. if i use rand()%35 than it picks numbers ...
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.
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]
>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.
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/13Originally 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
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.
i use the following function in my program :
now call my_rand(20); to get a random number between 1 and 20.Code:int my_rand(int max) { return 1+rand()%(max); }
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
in your program to get real random numbersCode:srand(time(NULL));
>...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]
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.what do you mean?
"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
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
well... that's what i thought thanks
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).
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.