SA-MP Forums Archive
Randomize numbers between int limits - 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: Randomize numbers between int limits (/showthread.php?tid=624409)



Randomize numbers between int limits - NeXoR - 16.12.2016

Hey, I need to randomize 30 numbers into an Array, each number is a randomized number between the int limits (both negative and positive)
Although, my code doesn't work, and on second thought it's not even logic.
Anyone ?
PHP код:
#include <a_samp>
new Array[30] = {0, ...};
main()
{
    
// Setting variables
    
for(new 0sizeof(Array); ji++) Array[i] = random(0x7FFFFFFF) + (-0x7FFFFFFF), printf("%d", Array[i]);




Re: Randomize numbers between int limits - Threshold - 16.12.2016

By 'doesn't work' you mean it doesn't give the desired result?

Well, all you really need to randomise is the difference between the two amounts. So lets say I want a value between -24 and +15. The difference between the two numbers is 24 + 15 = 39. So:
PHP код:
random(40) + -24 
PHP код:
new Array[30] = {0, ...}; 
main() 

    
// Setting variables 
    
for(new 0sizeof(Array); i++) Array[i] = random((maxvalue minvalue) + 1) + minvalueprintf("%d", Array[i]); 

Also, sizeof is not a function. Think of it as a macro. It gets determined upon compilation.


Re: Randomize numbers between int limits - NeXoR - 16.12.2016

Quote:
Originally Posted by Threshold
Посмотреть сообщение
By 'doesn't work' you mean it doesn't give the desired result?

Well, all you really need to randomise is the difference between the two amounts. So lets say I want a value between -24 and +15. The difference between the two numbers is 24 + 15 = 39. So:
PHP код:
random(40) + -24 
PHP код:
new Array[30] = {0, ...}; 
main() 

    
// Setting variables 
    
for(new 0sizeof(Array); i++) Array[i] = random((maxvalue minvalue) + 1) + minvalueprintf("%d", Array[i]); 

Also, sizeof is not a function. Think of it as a macro. It gets determined upon compilation.
Still all values are negative, maybe the variable type of random() is int ? (so maxvalue - minvalue is too high))


Re: Randomize numbers between int limits - Threshold - 16.12.2016

What are you using for maxvalue and minvalue?


Re: Randomize numbers between int limits - NeXoR - 16.12.2016

Quote:
Originally Posted by Threshold
Посмотреть сообщение
What are you using for maxvalue and minvalue?
PHP код:
random((0x7FFFFFFF - (-0x7FFFFFFF)) + 1) + (-0x7FFFFFFF



Re: Randomize numbers between int limits - Threshold - 16.12.2016

Well obviously using the maximum value for a 32-bit integer and having it doubled is going to cause issues. Use smaller values.

0x7FFFFFFF - (-0x7FFFFFFF) = 0x7FFFFFFF + 0x7FFFFFFF = 2 * 0x7FFFFFFF

You can't go above 0x7FFFFFFF :>


Re: Randomize numbers between int limits - NeXoR - 16.12.2016

Quote:
Originally Posted by Threshold
Посмотреть сообщение
Well obviously using the maximum value for a 32-bit integer and having it doubled is going to cause issues. Use smaller values.

0x7FFFFFFF - (-0x7FFFFFFF) = 0x7FFFFFFF + 0x7FFFFFFF = 2 * 0x7FFFFFFF

You can't go above 0x7FFFFFFF :>
K, thanks