aiaf Posted April 30, 2017 Posted April 30, 2017 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. Quote I made this with Leadwerks/UAK: Structura | Stacky Desktop Edition Website: Binary Station
Thirsty Panther Posted April 30, 2017 Posted April 30, 2017 Math::Random is a pseudo random number generator. This will explain it better than me. Quote
aiaf Posted April 30, 2017 Author Posted April 30, 2017 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)); } Quote I made this with Leadwerks/UAK: Structura | Stacky Desktop Edition Website: Binary Station
macklebee Posted April 30, 2017 Posted April 30, 2017 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. Quote Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590 LE / 3DWS / BMX / Hexagon macklebee's channel
aiaf Posted April 30, 2017 Author Posted April 30, 2017 It should have is confusing. Thanks for help, seem ill use the c++ random facilities. Quote I made this with Leadwerks/UAK: Structura | Stacky Desktop Edition Website: Binary Station
macklebee Posted April 30, 2017 Posted April 30, 2017 I lied to you! 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. 2 Quote Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590 LE / 3DWS / BMX / Hexagon macklebee's channel
aiaf Posted April 30, 2017 Author Posted April 30, 2017 Works! Should be added in api documentation: http://www.leadwerks.com/werkspace/page/api-reference/_/math/ Quote I made this with Leadwerks/UAK: Structura | Stacky Desktop Edition Website: Binary Station
Einlander Posted May 1, 2017 Posted May 1, 2017 Amazingly I get the same first number every time even if I use the time as the seed. :/ Quote
AggrorJorn Posted May 1, 2017 Posted May 1, 2017 Amazingly I get the same first number every time even if I use the time as the seed. :/ Are you using system time or application time? 1 Quote
Recommended Posts
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.