[REP++] Generating some numbers - 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: [REP++] Generating some numbers (
/showthread.php?tid=633284)
[REP++] Generating some numbers -
buburuzu19 - 28.04.2017
I need some help in generating some random numbers.
For example i have a var which defines the number of generations. For example 5, if it's 5 then i need 5 generated random numbers from 1-10 but distinct numbers.
Any ideas?
Thanks.
Re: [REP++] Generating some numbers -
Eoussama - 28.04.2017
Use
random function and a
loop
Re: [REP++] Generating some numbers -
buburuzu19 - 28.04.2017
Quote:
Originally Posted by Eoussama
Use random function and a loop
|
And how about making them distinct?
Re: [REP++] Generating some numbers -
Dayrion - 28.04.2017
Check if the number is already attributed?
Re: [REP++] Generating some numbers -
SyS - 28.04.2017
Quote:
Originally Posted by buburuzu19
And how about making them distinct?
|
add each generated values to an array. On each generation check the value present in array or not.If it present do generation process again (loop).
Re: [REP++] Generating some numbers -
Dayrion - 28.04.2017
I'm pretty sure it's optimizable :
PHP код:
DistinctNumber(number)
{
new
string[50],
count,
bool:sameNB = false;
do
{
string[count] = random(number);
for(new i; i < number; i++)
{
if(count == i)
continue;
if(string[count] == string[i])
sameNB = true;
}
if(!sameNB)
count++;
sameNB = false;
}
while(count != number);
return string;
}
Also, change 50 by the max number you will generate in your script
Re: [REP++] Generating some numbers -
SyS - 28.04.2017
Quote:
Originally Posted by Dayrion
I'm pretty sure it's optimizable :
PHP код:
DistinctNumber(number)
{
new
string[50],
count,
bool:sameNB = false;
do
{
string[count] = random(number);
for(new i; i < number; i++)
{
if(count == i)
continue;
if(string[count] == string[i])
sameNB = true;
}
if(!sameNB)
count++;
sameNB = false;
}
while(count != number);
return string;
}
Also, change 50 by the max number you will generate in your script
|
break from the loop if the match is found as further iteration is not required.