Random #'s in C++
Does anyone know where I can find a tutorial about generating random numbers in C++?
Check out Drunken Brawl at http://www.angelfire.com/games6/drunken_brawl!
include "time.h"
then you have to seed it, to make it a differnet random every time by this syntax...
float sec;
time(&sec);
srand(sec);
from this point, you make calls to rand(), which returns a value between 0-1. To make it a larger number you could say things like...
int num = rand()%18; //gives random number between 0 and 18 (not including 18)
hope i helped ya out
then you have to seed it, to make it a differnet random every time by this syntax...
float sec;
time(&sec);
srand(sec);
from this point, you make calls to rand(), which returns a value between 0-1. To make it a larger number you could say things like...
int num = rand()%18; //gives random number between 0 and 18 (not including 18)
hope i helped ya out
If barbie is so popular, why do you have to buy her friends?
December 16, 2002 09:03 PM
srand((unsigned)time(0)); to seed.
and rand() returns a number between 0 and RAND_MAX, inclusive.
and both srand and rand are from the C standard library, not C++. there is the STL''s random_shuffle, but it might not be what you want.
and rand() returns a number between 0 and RAND_MAX, inclusive.
and both srand and rand are from the C standard library, not C++. there is the STL''s random_shuffle, but it might not be what you want.
int RandomInt(int min, int max) { if(min>max) return RandomInt(max, min); else return std::rand()%(max-min+1)+min; } float RandomFloat(float min, float max) { return std::rand()*(max-min)/static_cast<float>(RAND_MAX)+min; }
[edited by - smart_idiot on December 16, 2002 10:24:07 PM]
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Yes but what if you need 30 random numbers all in the same millisecond? Putting the same time into the equation should always return the same random number right?
~Vendayan
~Vendayan
"Never have a battle of wits with an unarmed man. He will surely attempt to disarm you as well"~Vendayan
const SHNUM = max #;
int picknum()
{
int number;
number = (rand() % SHNUM);
return(number);
}
then to actually put it use, for example I''ll store the random # in a variable, so it could be used for like a number guessing game or something:
srand((unsigned)time(NULL));
int theNumber = picknum() + 1;
this makes the variable theNumber store a random number between 1, and whatever number you used for SHNUM
int picknum()
{
int number;
number = (rand() % SHNUM);
return(number);
}
then to actually put it use, for example I''ll store the random # in a variable, so it could be used for like a number guessing game or something:
srand((unsigned)time(NULL));
int theNumber = picknum() + 1;
this makes the variable theNumber store a random number between 1, and whatever number you used for SHNUM
"You probably use those fat crayons made for retarded kids." - warbux
December 17, 2002 03:11 AM
you only need to seed with srand at the begining of the program, that way each time it is run you will get different numbers
how exactly does that work?
~Vendayan
~Vendayan
"Never have a battle of wits with an unarmed man. He will surely attempt to disarm you as well"~Vendayan
int seed = 0;void srand(int value) { seed = value;}int rand() { return seed++;}srand((int)time(NULL));for(int i=0;i<100;++i) { std::cout<<"random value: "<<rand()<<std::endl;}
this rand() is overly simplified, but i think you get the idea..
"take a look around" - limp bizkit
www.google.com
[edited by - davepermen on December 17, 2002 4:26:16 AM]
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia
My Page davepermen.net | My Music on Bandcamp and on Soundcloud
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement