2 registered members (VoroneTZ, TipmyPip),
1,316
guests, and 0
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
what kind of random() distribution?
#329044
06/17/10 07:00
06/17/10 07:00
|
Joined: Mar 2009
Posts: 25
kholis
OP
Newbie
|
OP
Newbie
Joined: Mar 2009
Posts: 25
|
i just want to know what kind of random distribution on random() function in lite-c? is it normal distribution or uniform distribution?
thanks
Last edited by kholis; 06/17/10 08:12.
|
|
|
Re: kind of random() distribution
[Re: kholis]
#329057
06/17/10 10:37
06/17/10 10:37
|
Joined: Feb 2009
Posts: 2,154
Damocles_
Expert
|
Expert
Joined: Feb 2009
Posts: 2,154
|
You could write your own random generator Here is one example from the Wiki page: http://en.wikipedia.org/wiki/Random_number_generation
m_w = <choose-initializer>; /* must not be zero */
m_z = <choose-initializer>; /* must not be zero */
uint get_random()
{
m_z = 36969 * (m_z & 65535) + (m_z >> 16);
m_w = 18000 * (m_w & 65535) + (m_w >> 16);
return (m_z << 16) + m_w; /* 32-bit result */
}
As a "random" seed you could use some System-time parameter or some mousepositions.
|
|
|
Re: kind of random() distribution
[Re: Damocles_]
#329151
06/18/10 03:33
06/18/10 03:33
|
Joined: Oct 2002
Posts: 2,256 Oz
Locoweed
Expert
|
Expert
Joined: Oct 2002
Posts: 2,256
Oz
|
What I have learned to do, is put random_seed(0) in the main function while-wait(1) loop and it works well like that.
function main() { random_seed(0); .... while(1) { .... random_seed(0); wait(1); }
I would be surprised if you get uniform randomness with that (It will be uniform for one frame at a time actually). And random_seed() is a fast command, so not going to hurt your fps any. Should be simple solution for your random concerns.
Professional A8.30 Spoils of War - East Coast Games
|
|
|
Re: kind of random() distribution
[Re: DJBMASTER]
#329243
06/18/10 18:08
06/18/10 18:08
|
Joined: Mar 2009
Posts: 25
kholis
OP
Newbie
|
OP
Newbie
Joined: Mar 2009
Posts: 25
|
@DJBMASTER i've just read the theory but you have finished the code. big thank to you. here is the plot from your code. another great method is Ziggurat
|
|
|
Re: kind of random() distribution
[Re: kholis]
#329254
06/18/10 18:43
06/18/10 18:43
|
Joined: Nov 2007
Posts: 1,143 United Kingdom
DJBMASTER
Serious User
|
Serious User
Joined: Nov 2007
Posts: 1,143
United Kingdom
|
Great!
Yes, I was aware of the Ziggurat algorithm but it is harder to implement than the Box-Muller, although faster.
Last edited by DJBMASTER; 06/18/10 18:43.
|
|
|
Re: kind of random() distribution
[Re: DJBMASTER]
#329330
06/19/10 03:33
06/19/10 03:33
|
Joined: Oct 2002
Posts: 2,256 Oz
Locoweed
Expert
|
Expert
Joined: Oct 2002
Posts: 2,256
Oz
|
You can use the Box-Muller Transform...
float BoxMuller(float mean, float sd)
{
float x1, x2, w, y1; // polar coordinates
static float y2;
static int use_last = 0;
if (use_last)
{
y1 = y2;
use_last = 0;
}
else
{
do
{
x1 = 2.0 * random(1.0) - 1.0;
x2 = 2.0 * random(1.0) - 1.0;
w = x1 * x1 + x2 * x2;
}
while ( w >= 1.0 );
w = sqrt( (-2.0 * log( w ) ) / w );
y1 = x1 * w;
y2 = x2 * w;
use_last = 1;
}
return( mean + y1 * sd );
}
function main()
{
random_seed(0); // really random seed
float random_value = BoxMuller(5,2); // get random value from a Normal Distribution with a mean of 5 and standard deviation of 2.
}
Interesting. I will have to implement that. Always nice to learn something new. Thanks.
Professional A8.30 Spoils of War - East Coast Games
|
|
|
|