14.08.2013, 10:22
(
Последний раз редактировалось ProjectMan; 14.08.2013 в 11:21.
)
Hello, how do I generate a random integer but the integer must not be equal to the previous one that was generated. I cannot think properly now.
1 2 1 2 1 2 1 2 1 2 ...
... 1 1 ...
... 2 2 ...
new Old;
stock Randomize(integrer);
{
new int = random(integrer);
if(int == Old) return Randomize(integrer);
Old = int;
return int;
}
new oldInt;
stock randomEx(min, max) { new ints = random(max-min)+min; if(ints == oldInt) return randomEx(min, max); oldInt = ints; printf("%d",ints); return ints; }
#define randomEx(%0,%1) random(%1-%0) + %0
new oldInt;
stock randomDifferentNumber(min, max)
{
new ints;
do(ints = randomEx(min, max))
while(ints == oldInt);
oldInt = ints;
printf("%d", ints);
return ints;
}
What happens if you do:
pawn Код:
Also, you are ignoring context. Take this for example: pawn Код:
Код:
1 75 23 23 6 |
#define Randomize(%0,%1) random(%1-%0) + %0
stock randomEx(min, max)
{
new ints;
do
{
ints = Randomize(min, max+1); //must +1 if you want to include the upper boundary like this function suggests
}
while(ints == oldInt);
oldInt = ints;
printf("%d", ints);
return ints;
}
new oldInt[MAX_PLAYERS] = {-1, ...};
stock randomEx(playerid, min, max)
{
new ints;
do {
ints = Randomize(min, max+1);
}
while(ints == oldInt[playerid]);
oldInt[playerid] = ints;
printf("%d", ints);
return oldInt[playerid];
}
CMD:ur(playerid, params[])
{
SendClientMessageEx(playerid, 0xFF0000AA, "Random Number: %d", randomEx(playerid, 0, 100));
return 1;
}
new oldInt[2] = {-1, ...};
GetFreeInt()
{
for(new i = 0; i < sizeof oldInt; i++)
{
if(oldInt[i] == -1) return i;
}
return 0; //Start overwriting the array
}
stock randomEx(min, max)
{
new ints;
new s = GetFreeInt();
do {
ints = Randomize(min, max+1);
}
while(ints == oldInt[s] && ints == oldInt[s-1]);
printf("%d", ints);
oldInt[s] = ints;
return oldInt[s];
}
CMD:ur(playerid, params[])
{
SendClientMessageEx(playerid, 0xFF0000AA, "Random Number: %d", randomEx(1, 299));
return 1;
}