SA-MP Forums Archive
Random float. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Random float. (/showthread.php?tid=562354)



Random float. - Baltimore - 08.02.2015

Hello !

How to make a random float?

Between 0.50 and 2.15 for example.

++


Re: Random float. - Sascha - 08.02.2015

e.g. like this: no clue if there is a better solution
pawn Код:
new n1 = random(10000); //or whatever should be the max value infront of the .
new n2 = random(10000); //number behind the .
new string[50];
format(string, sizeof(string), "%d.%d", n1, n2);
new Float:randomfloat = floatstr(string);



Re : Re: Random float. - Baltimore - 08.02.2015

Quote:
Originally Posted by Sascha
Посмотреть сообщение
e.g. like this: no clue if there is a better solution
pawn Код:
new n1 = random(10000); //or whatever should be the max value infront of the .
new n2 = random(10000); //number behind the .
new string[50];
format(string, sizeof(string), "%d.%d", n1, n2);
new Float:randomfloat = floatstr(string);
How to make Between 0.50 and 2.15 for example with this?

++


Re: Random float. - HazardouS - 08.02.2015

Get a random number between 50 and 215 and divide it by 100, making sure that the result will be a float (result = random / 100.0; )


Re: Random float. - Sascha - 08.02.2015

pawn Код:
new n1 = random(3); //or whatever should be the max value infront of the .
new n2;
if(n1 == 0)
    n2 = random(50) + 50;
else if(n1 == 2)
    n2 = random(16);
else
    n2 = random(100);
new string[50];
format(string, sizeof(string), "%d.%d", n1, n2);
new Float:randomfloat = floatstr(string);
something like that (not tested)

or that
Quote:
Originally Posted by HazardouS
Посмотреть сообщение
Get a random number between 50 and 215 and divide it by 100, making sure that the result will be a float (result = random / 100.0; )



Re: Random float. - vinigas - 08.02.2015

Quote:
Originally Posted by Baltimore
Посмотреть сообщение
Hello !

How to make a random float?

Between 0.50 and 2.15 for example.

++
Код:
new Float:randomfloat = random(215 - 50) / 100.0  +  0.5;