Random number

Started by
19 comments, last by GameDev.net 18 years, 10 months ago
Hi, how do i make c++ create a random number from 1 to 9? Thanks
Advertisement
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));

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.

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.

Alex BakerComputer science is dealing with computers as much as astronomy is dealing with telescopes.
#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;}
don't forget,.....

#include <cstdlib>
or maybe it was
#include <math.h> hmmmmmm you guys would know..
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.
I teleported home one night; With Ron and Sid and Meg; Ron stole Meggie's heart away; And I got Sydney's leg. <> I'm blogging, emo style
Here's a link to a C++ Random Number Tutorial that explains how to do it in a clearer fashion than I could.
The number seems to be the the same everytime i restart my program. I've used everything you've posted
Quote:Original post by Yhack
The 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.

This topic is closed to new replies.

Advertisement