Hi There,
Quick question i need a way of getting a random number in the range of 15 to 88.
Im using visual studio.
Does anyone know of a way of doing this
thanks
Hi There,
Quick question i need a way of getting a random number in the range of 15 to 88.
Im using visual studio.
Does anyone know of a way of doing this
thanks
Do you know of a way of getting a random number (integer?) in the range of 0 to 73?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
no not with a range
Okay, read Prelude's article on using rand()
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Note where the values from your range come in. rand() produces a value in the range [0, RAND_MAX].Code:#include <stdlib.h> #include <time.h> int main() { int result; srand(time(NULL)); /* Do this once and only once before calling rand */ result = (rand() % (88-15)) + 15; /* Do this once each time you want a new random value */ }
This technique does not necessarily give a desirable distribution, but you haven't specified any such constraint.
It should be rand() % 74 + 15 if you want 15 to 88 inclusive.