Till now i was using the rand() / (RAND_MAX / X); style that the others
suggested but as i thought it for a while i discovered that with this type
the possibility of all numbers is not the same (the largest number is very
seldom to occur). So my new implementation is:
// Assuming C++
#include <iostream>
#include <cstdlib>
#include <ctime>
int main()
{
using namespace std;
srand(time(0)); // With this we feed srand() with a different seed every
time.
while(true)
{
// suppose we want a value between 0-X
const int X=2;
int number=rand() / (RAND_MAX / (X+1));
if(number==X+1)
continue;
cout<<number<<' ';
}
}
Ioannis
* Ioannis Vranos
* Programming pages: http://members.nbci.com/noicys
* Alternative URL: http://run.to/noicys
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...]On Behalf Of Jose Flores
> Sent: Tuesday, February 13, 2001 6:17 PM
> To: dev...@li...
> Subject: [Dev-C++] Random Number
>
>
> what is a good simple command to get a random number from 1 to 13? I keep
> getting the run around when I ask this question...
> Thanks
> Jose
>
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> http://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|