Jump to content
  • Advertisement
Sign in to follow this  
Yhack

Random number

This topic is 4875 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

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));

Share this post


Link to post
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 time

srand(time(0)); // Seeds the randomiser before it can be used - time ensures that it's different everytime the program runs

int 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 this post


Link to post
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 this post


Link to post
Share on other sites
#include <iostream> // For cout and endl
#include <cstdlib> // For srand and rand
#include <ctime> // For time

using 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 this post


Link to post
Share on other sites
don't forget,.....

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

Share this post


Link to post
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 this post


Link to post
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 this post


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

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!