:confused: please some1 can help me in writing a random number generator funtion......generates number from 1-10000...explain it..plz
:confused: please some1 can help me in writing a random number generator funtion......generates number from 1-10000...explain it..plz
Hello muadh jamal!
you can simply generate a random number by the function rand() in the stdlib directory. However c++ can't generate true random numbers as they are pseudo-random means having some pattern so we need to seed them for this we use the function srand(), and we must give a seeding-number in the argument so we usually give it the time as argument, like this
srand(time(0));
now if you want to give a range you can do it by this syntax
(minimum+rand())%maximum;
for example if you want to generate the numbers between 1 and 10,000 simply do it this way
(1+rand())%10000;
so the complete code should be like this along with a function to do it :
--------------------------------
#include <iostream.h>
#include <stdlib.h>
int generate(int,int); //prototype for function
main(){
srand(time(0)); // Seed the random function
int min=1,max=10000; // variable to store max and min range
int number=generate(min,max);
cout << "The generated number is : "<<number;
}
int generate(int min,int max){
int num=(min+rand())%max;
return num;
}
-----------------------------------
You can alter the range by altering the min and max variables
Hope it helps
Regards,
QaiS
now if you want to give a range you can do it by this syntax
(minimum+rand())%maximum;
for example if you want to generate the numbers between 1 and 10,000 simply do it this way(1+rand())%10000;
Not quite. it's minimum + (rand() % maximum);
Not quite. it's
minimum + (rand() % maximum);
Not quite, its: minimum + (rand() % (maximum - minimum));
This will produce random numbers in the range minimum inclusive and maximum exclusive.
;)
Oops.... so it is. Thanks Sannie :D
Not quite, its:
minimum + (rand() % (maximum - minimum));
This will produce random numbers in the range minimum inclusive and maximum exclusive.
Not quite, it's
minimum + rand() / (RAND_MAX / (maximum - minimum) + 1 );
To ensure proper distribution.
:twisted:
(sorry, I couldn't help it :mrgreen:)
Not quite, it's
minimum + rand() / (RAND_MAX / (maximum - minimum) + 1 );
To ensure proper distribution.
:twisted:
(sorry, I couldn't help it :mrgreen:)
Actually, according to this very enlightening read, you're all wrong...
(sorry, I couldn't help it either) :mrgreen:
Oops.... so it is. Thanks Sannie
I guess its payback time... :mrgreen:
Not quite, it's
minimum + rand() / (RAND_MAX / (maximum - minimum) + 1 );
To ensure proper distribution.
(sorry, I couldn't help it )Actually, according to this very enlightening read, you're all wrong...
(sorry, I couldn't help it either)
I never said my code ensured proper distribution. Atleast it was logically correct. :D
this is what the linux man pages say (in part) about rand(3):
" ... The versions of rand() and srand() in the Linux C Library
use the same random number generator as random() and sran
dom(), so the lower-order bits should be as random as the
higher-order bits. However, on older rand() implementa
tions, the lower-order bits are much less random than the
higher-order bits.
In Numerical Recipes in C: The Art of Scientific Computing
(William H. Press, Brian P. Flannery, Saul A. Teukolsky,
William T. Vetterling; New York: Cambridge University
Press, 1992 (2nd ed., p. 277)), the following comments are
made:
"If you want to generate a random integer between 1
and 10, you should always do it by using high-order
bits, as in
j=1+(int) (10.0*rand()/(RAND_MAX+1.0));
and never by anything resembling
j=1+(rand() % 10);
(which uses lower-order bits)."
Random-number generation is a complex topic. The Numeri
cal Recipes in C book (see reference above) provides an
excellent discussion of practical random-number generation
issues in Chapter 7 (Random Numbers). ..."
Now we have all seen that our moderators are less than perfect:
// typical example for seeded random integers in a given range
#include <stdlib.h>
#include <stdio.h>
#include <time.h> // time()
int main(void)
{
int i, min, max, rn;
srand(time(NULL)); // random seed on most compilers
min = 1;
max = 10000;
printf("Ten random numbers from 1 to 10000\n\n");
for(i = 0; i < 10; i++)
{
rn = min + (int)((float)max*rand()/(RAND_MAX+(float)min));
printf("%d\n", rn);
}
return 0;
}
Now we have all seen that our moderators are less than perfect
And so are most that try one-upmanship ;)
No formatting
Poor commenting
Casts to float that are useless.
:rolleyes: