• Advertisement
Sign in to follow this  

quick question

This topic is 4872 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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.

Share this post


Link to post
Share on other sites
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
*G*

I saw no answers when i started writing my post, that was fast!

Share this post


Link to post
Share on other sites
sorry i should have said what language. C++ and i figured it out shortly after posting :P but thanx for the replys.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement