what kind of random() distribution?

Posted By: kholis

what kind of random() distribution? - 06/17/10 07:00

i just want to know what kind of random distribution on random() function in lite-c?
is it normal distribution or uniform distribution?

thanks
Posted By: Helghast

Re: kind of random() distribution - 06/17/10 07:25

i believe it's normal.
but set random_seed(0); in your main to get a different seed everytime.

regards,
Posted By: kholis

Re: kind of random() distribution - 06/17/10 09:38

i made a file generated with random() function and then plot it in matlab.

look like its a uniform distribution.

any other way to make it normal dist?

thanks
Posted By: Damocles_

Re: kind of random() distribution - 06/17/10 10:37

You could write your own random generator

Here is one example from the Wiki page:
http://en.wikipedia.org/wiki/Random_number_generation

Code:
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.
Posted By: Locoweed

Re: kind of random() distribution - 06/18/10 03:33

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.
Posted By: kholis

Re: kind of random() distribution - 06/18/10 16:01

Code:
function main()
{
  random_seed(0);
  var filehandle = file_open_write("myfile.txt");
  while(1)
  {
    file_var_write(filehandle, random(1));
    random_seed(0);
    wait(1);
  }
}



i've plotted myfile.txt with contain about 3000 number. and still got a uniform distribution.

Posted By: DJBMASTER

Re: kind of random() distribution - 06/18/10 16:20

You can use the Box-Muller Transform...
Code:
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.
}


Posted By: kholis

Re: kind of random() distribution - 06/18/10 18:08

@DJBMASTER
i've just read the theory but you have finished the code. tongue
big thank to you.

here is the plot from your code.


another great method is Ziggurat
Posted By: DJBMASTER

Re: kind of random() distribution - 06/18/10 18:43

Great!

Yes, I was aware of the Ziggurat algorithm but it is harder to implement than the Box-Muller, although faster.


Posted By: Locoweed

Re: kind of random() distribution - 06/19/10 03:33

Originally Posted By: DJBMASTER
You can use the Box-Muller Transform...
Code:
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.
© 2024 lite-C Forums