Gamestudio Links
Zorro Links
Newest Posts
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
Help with plotting multiple ZigZag
by degenerate_762. 04/30/24 23:23
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (7th_zorro, dr_panther), 1,211 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
firatv, wandaluciaia, Mega_Rod, EternallyCurious, howardR
19050 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
what kind of random() distribution? #329044
06/17/10 07:00
06/17/10 07:00
Joined: Mar 2009
Posts: 25
kholis Offline OP
Newbie
kholis  Offline 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] #329045
06/17/10 07:25
06/17/10 07:25
Joined: Jan 2004
Posts: 3,023
The Netherlands
Helghast Offline
Expert
Helghast  Offline
Expert

Joined: Jan 2004
Posts: 3,023
The Netherlands
i believe it's normal.
but set random_seed(0); in your main to get a different seed everytime.

regards,


Formerly known as dennis_fantasy
Portfolio - http://www.designorhea.com/
Project - http://randomchance.cherrygames.org/
Re: kind of random() distribution [Re: Helghast] #329052
06/17/10 09:38
06/17/10 09:38
Joined: Mar 2009
Posts: 25
kholis Offline OP
Newbie
kholis  Offline OP
Newbie

Joined: Mar 2009
Posts: 25
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

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_ Offline
Expert
Damocles_  Offline
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

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.

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
L
Locoweed Offline
Expert
Locoweed  Offline
Expert
L

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: Locoweed] #329226
06/18/10 16:01
06/18/10 16:01
Joined: Mar 2009
Posts: 25
kholis Offline OP
Newbie
kholis  Offline OP
Newbie

Joined: Mar 2009
Posts: 25
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.


Re: kind of random() distribution [Re: kholis] #329228
06/18/10 16:20
06/18/10 16:20
Joined: Nov 2007
Posts: 1,143
United Kingdom
DJBMASTER Offline
Serious User
DJBMASTER  Offline
Serious User

Joined: Nov 2007
Posts: 1,143
United Kingdom
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.
}



Re: kind of random() distribution [Re: DJBMASTER] #329243
06/18/10 18:08
06/18/10 18:08
Joined: Mar 2009
Posts: 25
kholis Offline OP
Newbie
kholis  Offline OP
Newbie

Joined: Mar 2009
Posts: 25
@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

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 Offline
Serious User
DJBMASTER  Offline
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
L
Locoweed Offline
Expert
Locoweed  Offline
Expert
L

Joined: Oct 2002
Posts: 2,256
Oz
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.


Professional A8.30
Spoils of War - East Coast Games

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1