Public Group

# Random number

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

## Recommended Posts

Hi, how do i make c++ create a random number from 1 to 9? Thanks

##### Share on other sites
There are several ways to generate numbers in a specified range, some better than others. One method:

random_integer = (rand()%10);

This will generate numbers in the range 0 to 9. To use a non-zero baseline you can do the following:

random_integer = 20 + (rand()%10);

This will generate numbers in the range 20 to 29.

The above method of producing pseudo random numbers is poor because it only uses the low-order bits of the number returned from rand() and these may be less random for some pseudo random number generator implementations.

For a better method you should prefer the following:

random_integer = 20 + int(10.0 * rand()/(RAND_MAX+1.0));

##### Share on other sites
Here's how you would do it:

#include <iostream>#include <cstdlib>#include <ctime> // Allows for better randomisation based on the current system timesrand(time(0)); // Seeds the randomiser before it can be used - time ensures that it's different everytime the program runsint RandomNumber=rand()%9+1; // 1 is the lowest it can be - 9 is the highest

EDIT: Balls, got beaten again! Anyway, his way's better. Mine's just simpler but not as powerful.

##### Share on other sites

ciao,

try the most simple way:

int nNumber = rand() % 9;

disclaimer: this is the most simple way; and you shouldn't expect too much from this construction.

##### Share on other sites
#include <iostream> // For cout and endl#include <cstdlib> // For srand and rand#include <ctime> // For timeusing namespace std; // Make the code easyer to read.int main(){	srand(time(0)); // Do this ONCE.	int x = rand()%9+1; // This isnt very good but it works.	cout << x << endl; // Show the number.	return 0;}

##### Share on other sites
don't forget,.....

#include <cstdlib>
or maybe it was
#include <math.h> hmmmmmm you guys would know..

##### Share on other sites
Here is what you need to know.

RAND_MAX is a constant that gives the highest number generated by rand();
rand(): a function that returns an integer between 0 and RAND_MAX
srand(int): "Seeds" the rand function. An integer is used as a seed.
Typically, this integer is generated by using some kind of time function.

#include <cstdlib>#include <ctime>#include <iostream>int main(){   int upperBound = 9;   int lowerBound = 1;    //Initial seed (typically only once in a program)    srand((unsigned)time( NULL ) );    //prints 5 floating point number between lowerBound and upperBound.    for (int i = 0; i < 5; i++) {        cout << (((float)rand()/ RAND_MAX) + lowerBound) * (upperBound-lowerBound);    }    return 0;}

Dissecting it :

(float)rand()/ RAND_MAX gives a number between 0 and 1, since RAND_MAX is the highest possible value.

We then add the lower bound to that number, and multiply it by the difference between the upper and lower bound to get a number i a given range.

##### Share on other sites
Here's a link to a C++ Random Number Tutorial that explains how to do it in a clearer fashion than I could.

##### Share on other sites
The number seems to be the the same everytime i restart my program. I've used everything you've posted

##### Share on other sites
Quote:
 Original post by YhackThe number seems to be the the same everytime i restart my program. I've used everything you've posted

You need to do the srand() call.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 28
• 16
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
634103
• Total Posts
3015539
×