Randomize numbers between int limits
#1

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]);

Reply
#2

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.
Reply
#3

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))
Reply
#4

What are you using for maxvalue and minvalue?
Reply
#5

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

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 :>
Reply
#7

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
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)