quick question
quick question. how do I get the computer to generate a random number between 2 numbers. lets say for example, it randomly chooses between 1-3. how would i do this. any help is greatly appreciated.
i don't know if this is what your looking for but
#include<iostream>#include<ctime>#include<cstdlib>using namespace std;int main(){ srand(time(0)); int num = ((rand() % 3) + 1); cout<<num; int x; cin>>x; return 0;}
Language? Platform?
The typical C solution is to obtain the modulus of the rand() function and the upper bound. This has problems with certain implementations of the C runtime because the lower bits have less randomness than the overall value.
The typical C solution is to obtain the modulus of the rand() function and the upper bound. This has problems with certain implementations of the C runtime because the lower bits have less randomness than the overall value.
In C.
number=rand()%2; //0-1
number=rand()%3; //0-2
number=rand()%4; //0-3
number=(rand()%2)+1; //1-2
number=(rand()%2)+3; //3-4
number=rand()%2; //0-1
number=rand()%3; //0-2
number=rand()%4; //0-3
number=(rand()%2)+1; //1-2
number=(rand()%2)+3; //3-4
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement