Thursday, July 28, 2022

This killer C:macro make, C random like it would be JS:random

 Javascript random is nice, because it is between 0 and 1; So you can like:

 Math.random()*sum where you get then numbers between 0 and the sum

like if sum is 7 it give's random's 0-7. And if you want transfer it out of zero you just add numbers to it. 


  So it good and easy, but Random in C gives BIG numbers it harder to use, here how you alter it similar to JS-random:

#define JSrandom (float)(rand()%32767)/32767.00

Then use like JS

random_num=(int)JSrandom*sum + 1;

But you must, before use that JSrandom, make that time seeds thingy, as usually, that make random more random

srand((unsigned int)time(&t));

How it works? Lowest number that C random gives is 32767 that what they promise. If you then use it to "%(modulo)" random it ensure that all numbers are lower than it(32767) or equal. When it(the calculation) be then divided by 32767 it make so that what was before 32767 is now 1 and all other numbers be between 0 to 1.



 

3D BLog has return

Go  to Check    https://thereal3dblog.com/