SA-MP Forums Archive
[Question] 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)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: [Question] Random Float? (/showthread.php?tid=183477)



[Question] Random Float? - Maxips2 - 15.10.2010

I tried this code (just an example):

pawn Code:
new
    Float:randx = 5.0,
    Float:res = random(randx);
And I'm getting a warning of "Tag Mismatch".
Any ideas how to make a random float?


Re: [Question] Random Float? - mrmuffins - 15.10.2010

Cause the function random only supports integers, not floats, and your using a float as a value.
So tag mismatch.

Now you can try to remove the tag check on the float using _: aka Tag Override.
It will remove the warning, however I am not a fan of floats, but testing this out comes out to very small random floats.

(For example your using 5.0 as max, but some of my results were 0.0000345, 0.000018 etc..., that's small)

Heres my code:

pawn Code:
for (new i = 0; i < 1000; i++) {
        new Float:randx = 5.0, res = random(_:randx);
        printf("%f", res);
    }
Now you might be wondering why I'm not storing res in a Float, but outputting it as a float, is cause it just doesn't work. You get massive values such as 894232.123455 which is obviously Much larger then 5.0...
Only this way seems to make them somewhat of random floats within the size.
(I could be completely wrong but meh, floats aren't my thing)


Re: [Question] Random Float? - Maxips2 - 15.10.2010

Okay, its working, but a few problems..
1. Very small results (as you said before: 0.0000something)
2. It prints lots of numbers (when I type /test) - I guess thats because of the loop
Well, I found my own way to random that code:

pawn Code:
new Float:randx1 = random(4);
new Float:randy1 = random(8);
new Float:randx = floatsqroot(floatdiv(randx1, 1.23));
new Float:randy = floatsqroot(floatdiv(randy1, 1.63));
Thanks anyway