The custom random
#1

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

If you don't understand, here is what I mean:

Generate between number 1 and 2 will return
Код:
1
2
1
2
1
2
1
2
1
2
...
But,

Код:
...
1  
1
...
or

Код:
...
2
2
...
should never occur because if it occurs, it means that the previous integer is the same as the next integer... I hope you understood what I want.
Reply
#3

By adding a new variable maybe:
pawn Код:
new Old;
stock Randomize(integrer);
{
     new int = random(integrer);
     if(int == Old) return Randomize(integrer);
     Old = int;
     return int;
}
Then you can use Randomize(Any Integrer Here); in your script.
Reply
#4

I have modified your code to this:

Global variable:
Код:
new oldInt;
Stock:
Код:
stock randomEx(min, max)
{
     new ints = random(max-min)+min;
     if(ints == oldInt) return randomEx(min, max);
     oldInt = ints;
     printf("%d",ints);
     return ints;
}
It crashes.
Reply
#5

Compiler crashes, or the game?
The compiler works fine with me.
Reply
#6

pawn Код:
#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;
}
Reply
#7

Quote:
Originally Posted by Dragonsaurus
Посмотреть сообщение
Compiler crashes, or the game?
The compiler works fine with me.
Try my code, go in game, put it in a command or something and see the output. It freezes the server.
Reply
#8

Quote:
Originally Posted by ******
Посмотреть сообщение
What happens if you do:

pawn Код:
CustomRandom(1);
? And I seriously doubt that code is hanging the server, unless you are using a range with only 1 number (as in my example above, hence my example above).

Also, you are ignoring context. Take this for example:

pawn Код:
CMD:ur(playerid, params[])
{
    SendClientMessageEx(playerid, 0xFF0000AA, "Random Number: %d", CustomRandom(100));
    return 1;
}
The will display a random number to the player every time they use it, so they could get the following output if they use it repeatedly:

Код:
1
75
23
23
6
Err, there's a repeating number in there, what gives? Simple, another player may have used the command between the two times this player used the command, in which case the constraint that no two sequential numbers are the same is preserved globally, but not for any one player. Again, what should happen in this case? The problem is actually more general even than this - any two different functions that both call this function can get repeating sequences if they call the random function alternately. Is that what you want? If not I think you need to think a bit more about what you actually want (I'd suggest a "previous value" parameter so that each call can handle their unique sequences separately).
My mistake, apparently I overlooked the function randomEx(min,max). This function will not include the max boundary as a possible value. Such that doing something like this: randomEx(2600,2601) will output 2600 once and it will freeze the server (possibly because of the infinite loop since it can't reach 2601). So for someone else who might be searching what I intended to find by creating this thread, I will leave this one for you. I have modified [XST]O_x's code into something working:

pawn Код:
#define Randomize(%0,%1) random(%1-%0) + %0
Stock:
pawn Код:
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;
}
Thanks all of you for your wonderful help.
Reply
#9

You can just use a player variable.
pawn Код:
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;
}
Reply
#10

pawn Код:
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;
}
I don't have any better idea.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)