Sign in to follow this  
Dransic

quick question

Recommended Posts

Dransic    122
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.

Share this post


Link to post
Share on other sites
hothead    767
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;
}

Share this post


Link to post
Share on other sites
xor    516
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this