Jump to content

Math::Random(float, float) ?


aiaf
 Share

Recommended Posts

Have a look at the output of this program run for a couple of times:

 

int main(int argc, const char *argv[])
{
srand(time(NULL));

for (int i = 0; i < 5; i++) {
 printf("%d ", rand() % 360);
}

printf("\n");

for (int i = 0; i < 5; i++) {
 printf("%f ", Math::Random(0.0, 360.0));
}

 

 

159 153 117 103 57

293.300507 48.771721 326.085083 300.603088 45.715256

 

168 350 54 276 33

293.300507 48.771721 326.085083 300.603088 45.715256

 

181 136 214 26 243

293.300507 48.771721 326.085083 300.603088 45.715256

 

I dont understand what happens with Math::Random, it returns the same numbers.

Using rand%360 looks ok.

I made this with Leadwerks/UAK:

Structura Stacky Desktop Edition

Website:

Binary Station

Link to comment
Share on other sites

Yes and i use the current time as a seed.Still it doesnt change the values.

 

Below is a pseudo random generator from c++, it doesnt behave as Math::Random i just tested:

 

std::random_device seed;
std::mt19937 gen(time(NULL));
std::uniform_int_distribution<int> dist(0, 360);
for (int i = 0; i < 5; i++)
{
printf("%d ", dist(gen));
}

I made this with Leadwerks/UAK:

Structura Stacky Desktop Edition

Website:

Binary Station

Link to comment
Share on other sites

Usually, when a program starts, it initializes the generator with a fixed seed. That means that, every time you run your program, it generates the same sequence of pseudo-random numbers.

 

]LE's Math:Random() doesn't have a way to change the seed.

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

I lied to you! smile.png

 

It looks like Josh has added that at some point but doesn't have it documented yet. I can see from a lua global variable dump there is this command:

 

Math:SeedRandom(float)

 

EDIT-- just tried it and it appears to work properly. I get different numbers each time i run the program.

  • Upvote 2

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...