[gameprogrammer] Re: Making random numbers in C++
- From: grant hallman <unilogic@xxxxxxxxx>
- To: gameprogrammer@xxxxxxxxxxxxx
- Date: Sat, 03 Jan 2009 01:26:01 -0500
Hi, and happy new year all :)
I happened to notice this, and thought i'd drop another 2 cents on the
pile. You seem to be using the system clock to re-seed the RNG each time u
access it. I don't know the insides of this particular RNG, but don't they
usually get seeded once and then called repeatedly? Depends on the
algorithm used, but if that's how it's designed, you won't get good quality
RNs by continually reseeding it from the clock, you might as well just use
the LSB's of the clock. And FWIW, i've rarely met a RNG provided with a
language, that i trust to maintain a long good-quality string. They tend to
be under-tested, likely because it's sometimes tricky to tell a good RNG
from a poor one, until it's well into the project and u start seeing funny
results. Think about making your own, they aren't hard, and that way u know
what you're dealing with. And if good quality matters, test it yourself,
not just a simple pocket histogram, but at least 2-D correlates. A 2-D
scattergram will tell u a lot.
All algorithmic RNGs eventually repeat. A quality one will have a very long
repeat cycle, and no discernable patterns.
Good luck :) Grant
Scene from undergrad math class...
Professor: Would someone please give me a random number?
Student 1: 5
Professor: No, no! More random please! Anyone?
Student 2: 17?
Professor: Much better!
Huh?
At 09:59 PM 1/1/2009 +0500, you wrote:
Thanks for the reply, guyz
I just figured it out.
What I was doing , was this:
for (int i=0;i<10000;i++)
{
//some code
srand (time(NULL));
//some code
}
whereas srand should be out of the loop.
this srand was iterating again and again,
and was generating 1 integer for 1 sec.
thanks for your reply, I really appriciate that.
> Date: Wed, 31 Dec 2008 20:11:37 +0000
> From: brian.ripoff@xxxxxxxxx
> To: gameprogrammer@xxxxxxxxxxxxx
> Subject: [gameprogrammer] Re: Making random numbers in C++
>
> That should not happen. Can you post a complete example that
> demonstrates this behaviour. My computer can generate a million random
> numbers in a handful of milliseconds, according to a quick,
> unoptimised program I threw together, compiled without optimisation
> either.
>
> On Wed, Dec 31, 2008 at 7:53 PM, Azeem Zaheer <azeem_841@xxxxxxxxxxx>
wrote:
> > I am making a program in which I need about 10,000 "random" numbers
ranging
> > from 1 to 10.I have to make it in
> > C++.So, when I use srand(time(NULL)); it works..but it is tooo slow.
> > I takes 1 whole second to process and generate 1 number.
> > I will have to wait 1000 secs, which is hell of a time.Please suggest me
> > someway to make random numbers
> > more quicker.Just tell me how to make 1 random number in 1 millisecond or
> > even less.
>
> ---------------------
> To unsubscribe go to http://gameprogrammer.com/mailinglist.html
>
>
----------
Get easy photo sharing with Windows Live? Photos. Drag n? drop
Other related posts: