Raised This Month: $51 Target: $400
 12% 

preventing a random from choosing the same thing twice.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Dragonshadow
BANNED
Join Date: Jun 2008
Old 07-24-2009 , 07:16   preventing a random from choosing the same thing twice.
Reply With Quote #1

I have a getrandomint(0, 13)

I want it so that if it rolls say a 6, its next roll can NOT be the same number.

I have an array of strings, and use something like this
PHP Code:

#define numphrases 10

new const String:phrases[numphrases][] = {
    
"one",
    
"two",
    
"three",
    
"four",
    
"five",
    
"six",
    
"seven",
    
"eight",
    
"nine",
    
"ten"
};

Format(messagesizeof(message), phrases[GetRandomInt(0numphrases-1)]);

SayText2(clientmessage); 
to print them.

Well I want it so if it prints eight from the "roll", it can't print eight for the next three "rolls"

Does that make sense?
Dragonshadow is offline
Gachl
BANNED
Join Date: Feb 2009
Old 07-24-2009 , 07:35   Re: preventing a random from choosing the same thing twice.
Reply With Quote #2

PHP Code:
new iLastRandom[3];

public 
GetBetterRandomInt(minmax)
{
    if (
max sizeof(iLastRandom))
        return 
GetRandomInt(minmax);
    new 
iRandom GetRandomInt(minmax);
    new 
bool:bWrong true;
    while (
bWrong)
    {
        
bWrong false;
        for (new 
0sizeof(iLastRandom); i++)
        {
            if (
iLastRandom[i] == iRandom)
            {
                
bWrong true;
                break;
            }
        }
        if (
bWrong)
            
iRandom GetRandomInt(minmax);
    }
    for (new 
sizeof(iLastRandom) - 2>= 0j++)
    {
        
iLastRandom[j+1] = iLastRandom[j];
    }
    
iLastRandom[0] = iRandom;
    return 
iRandom;

I have *no* idea if this works, but if you call GetBetterRandomInt(min, max) it should give you a random number between min and max which is not in the last sizeof(iLastRandom) results.
eg. usage:
PHP Code:
Format(messagesizeof(message), phrases[GetBetterRandomInt(0numphrases-1)]); 
edit: fixed small error in code
edit2: fixed huge error in code

Last edited by Gachl; 07-24-2009 at 07:53. Reason: fixed small error in code, +example, fixed huge error in code
Gachl is offline
Dragonshadow
BANNED
Join Date: Jun 2008
Old 07-24-2009 , 08:03   Re: preventing a random from choosing the same thing twice.
Reply With Quote #3

Well before I have a chance to test it, I'm using this in more than one timer with more than one array and more than one client... Will that screw anything up?
Dragonshadow is offline
Gachl
BANNED
Join Date: Feb 2009
Old 07-24-2009 , 08:10   Re: preventing a random from choosing the same thing twice.
Reply With Quote #4

Umm, well if timer1, timer2, timer3 and timer4 and then timer1 again run this function its possible that timer1 can get the same random int twice, because timer2, timer3 and timer4 pushed the first int of timer1 out of the duplicate array
Gachl is offline
Dragonshadow
BANNED
Join Date: Jun 2008
Old 07-26-2009 , 09:24   Re: preventing a random from choosing the same thing twice.
Reply With Quote #5

Bump, Gachl seems to be busy and hasn't responded to my last pm, so this is what we have so far:

PHP Code:
new iLastRandom[12][3]; 

public 
GetBetterRandomInt(minmaxrid

    if (
max sizeof(iLastRandom[])) 
        return 
GetRandomInt(minmax); 
    new 
iRandom GetRandomInt(minmax); 
    new 
bool:bWrong true
    while (
bWrong
    { 
        
bWrong false
        for (new 
0sizeof(iLastRandom[]); i++) 
        { 
            if (
iLastRandom[rid][i] == iRandom
            { 
                
bWrong true
                break; 
            } 
        } 
        if (
bWrong
            
iRandom GetRandomInt(minmax); 
    } 
    for (new 
sizeof(iLastRandom[]) - 2>= 0j++) 
    { 
        
iLastRandom[rid][j+1] = iLastRandom[rid][j]; 
    } 
    
iLastRandom[rid][0] = iRandom
    return 
iRandom

That is supposed to prevent each (uhh) thing I use it on from rolling the same thing.
Problem is it doesn't work at all.

PHP Code:
new random GetBetterRandomInt(0130);

Format(messagesizeof(message), "\x03%s\x01 Rolled a\x04 %i\x01!"clientnamerandom);

SayText2(clientmessage);
return 
Plugin_Continue
With that I can still roll the same number twice, even three times in a row.

Same this with:
PHP Code:
// Start of plugin
#define numroll 12

new const String:rollphrases[numroll][] = {
//stuff here
};
////////////////////////

// In function
Format(messagesizeof(message), rollphrases[GetBetterRandomInt(0numroll-11)], clientname);

SayText2(clientmessage);
return 
Plugin_Continue
I can "roll" the same string multiple times in a row.

Last edited by Dragonshadow; 07-26-2009 at 09:29.
Dragonshadow is offline
FeuerSturm
AlliedModders Donor
Join Date: Apr 2004
Old 07-26-2009 , 10:46   Re: preventing a random from choosing the same thing twice.
Reply With Quote #6

What I'd do is us the normal GetRandomInt, save the value in
a global indexed variable and compare it on the next roll...
roll again if it's the same, update the global variable with the new value if it isn't and be happy
FeuerSturm is offline
Dragonshadow
BANNED
Join Date: Jun 2008
Old 07-26-2009 , 12:37   Re: preventing a random from choosing the same thing twice.
Reply With Quote #7

Quote:
Originally Posted by FeuerSturm View Post
What I'd do is us the normal GetRandomInt, save the value in
a global indexed variable and compare it on the next roll...
roll again if it's the same, update the global variable with the new value if it isn't and be happy
Well with the rolling 0-13 that would be fine, but I for the strings I want to "blacklist" it for the next 3 rolls, that way I don't roll something, roll something else, then roll the first thing again.
Dragonshadow is offline
FeuerSturm
AlliedModders Donor
Join Date: Apr 2004
Old 07-26-2009 , 13:18   Re: preventing a random from choosing the same thing twice.
Reply With Quote #8

you can still do that with using a 2D array

example:

PHP Code:
new g_LastRolls[MAXPLAYERS+1][3]
new 
g_LastRollPos[MAXPLAYERS+1
then on "OnClientPostAdminCheck"
PHP Code:
g_LastRolls[client][0] = -1
g_LastRolls
[client][1] = -1
g_LastRolls
[client][2] = -1
g_LastRollPos
[client] = -
then once you have a "randomnum"
PHP Code:
new bool:foundunused false
for(new 03i++)
{
 if(
g_LastRolls[client][i] != randomnum)
 {
  
// we found an unused number!
  
if(g_LastRollPos[client] == 2)
  {
   
g_LastRollPos[client] = 0
 
  
}
  else
  {
   
g_LastRollPos[client]++
  }
  
g_LastRolls[client][g_LastRollPos[client]] = randomnum
  foundunused 
true
 
}
}
if(
foundunused)
{
 
// do code with your found random number here!
}
else
{
 
// the random number was already used within the last 3 rolls

Basically that should work, but as I just wrote that up quickly I take
no responsability!

Last edited by FeuerSturm; 07-26-2009 at 13:36. Reason: added example
FeuerSturm is offline
Gachl
BANNED
Join Date: Feb 2009
Old 07-26-2009 , 13:27   Re: preventing a random from choosing the same thing twice.
Reply With Quote #9

well I've never tested my code I've sent you and I was away this weekend, but as soon as I have some spare time I'll test it and send you a working version.
Gachl is offline
Dragonshadow
BANNED
Join Date: Jun 2008
Old 07-27-2009 , 16:14   Re: preventing a random from choosing the same thing twice.
Reply With Quote #10

Quote:
Originally Posted by Gachl View Post
well I've never tested my code I've sent you and I was away this weekend, but as soon as I have some spare time I'll test it and send you a working version.
Cool cool
Dragonshadow is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 01:23.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode