random numbers

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
Randi
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 50
Joined: Sat Apr 24, 2010 1:32 pm
Location: Noobville

random numbers

Post by Randi »

I have a program that generates random numbers, for an array of classes, when I initialize the array it sets 2 variables to random integers. The problem I am having is the random integers are always the same for each instance of the class. I am currently using a do while loop to check to see if the numbers are the same, but the program takes a couple seconds until the integers are set, is there a faster way of doing this?

Code: Select all

player::player()
{
srand(static_cast<unsigned int>(time(0)));
health = rand()%50+1;
mana = rand()%70+1;
}
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: random numbers

Post by X Abstract X »

The numbers generated are the same for each instance because the time is probably the same each time you seed, assuming your array of players is small and takes little time to initialize.

You could seed once at the start of your program using the current time and generate all of the numbers without seeding again.
Live-Dimension
Chaos Rift Junior
Chaos Rift Junior
Posts: 345
Joined: Tue Jan 12, 2010 7:23 pm
Favorite Gaming Platforms: PC - Windows 7
Programming Language of Choice: c++;haxe
Contact:

Re: random numbers

Post by Live-Dimension »

No one can really tell you whats wrong without more code. I for one can't see an issue.

Random number generators aren't really that random. They are just a bunch of operations designed to look like they give a random number. They all start out from a chosen seed, usually it's the ms since unix epoch, or something similar. As such, giving the same seed to a RNG will give the same "random" number. I assume that's what is going on here.
Image
User avatar
ismetteren
Chaos Rift Junior
Chaos Rift Junior
Posts: 276
Joined: Mon Jul 21, 2008 4:13 pm

Re: random numbers

Post by ismetteren »

This may be a bit offtopic, but I recently found this site which seems pretty cool: http://www.random.org it generates random numbers from atmospheric noise and im pretty sure it has an API. This is probably an overkill, but it is also pretty cool :D
Image ImageImage Image
Scoody
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 65
Joined: Fri Feb 06, 2009 2:07 pm

Re: random numbers

Post by Scoody »

Just seed it once, ie. in main before anything else is initialized. Don't put it in a constructor that's called more than once like your player-class, as I understand.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: random numbers

Post by avansc »

im not sure what time gives, but if it give it in seconds, and you are initializing all the classes in a loop, you are seeding all of them with the same time.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Live-Dimension
Chaos Rift Junior
Chaos Rift Junior
Posts: 345
Joined: Tue Jan 12, 2010 7:23 pm
Favorite Gaming Platforms: PC - Windows 7
Programming Language of Choice: c++;haxe
Contact:

Re: random numbers

Post by Live-Dimension »

Exactly, this is what I failed to say last time. Hell, even ms since epoch is not enough. Some RNG's will change the seed each time since last use (Probably by adding/multiplying the last random number).
Image
Post Reply