AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Random number to each player. (https://forums.alliedmods.net/showthread.php?t=152910)

Paulster1022 03-16-2011 04:09

Random number to each player.
 
I am basically trying to set a random number to each player and also making sure it doesn't equal anyone else's number. It works of course, but my server is crashing. I know this because when i comment this function out, crashes don't exist. Is there a more efficient way of doing this? Thanks in advance.

Code:

public round_start() set_numbers()

// ============= Assigns a random number to each player ==================
set_numbers()
{
        new alive_humans = get_alivehumans()

        // Give Players random suspect #. If the same number as anyone else, redo loop.
        for(new i = 1; i <= maxplayers; i++)
            {
                if(is_user_alive(i))
                {
                        if(PlayerInfo[i] == HUMAN)
                        {
                                pnumber[i] = random_num(1, alive_humans)

                                for(new j = i - 1; j >= 1; j--)
                                    {
                                        if(pnumber[i] == pnumber[j])
                                        {
                                                i--
                                                break
                                        }
                                }               
                        }
                }
        }       
}

get_alivehumans()
{
        new alive_humans
        alive_humans = 0
       
        for(new id = 1; id <= maxplayers; id++)
        {
                if (is_user_alive(id) && PlayerInfo[id] == HUMAN)
                        alive_humans++
        }
       
        return alive_humans
}


schmurgel1983 03-16-2011 04:36

Re: Random number to each player.
 
try this one
PHP Code:

set_numbers()
{
    new 
alive_humans get_alivehumans()

    
// Give Players random suspect #. If the same number as anyone else, redo loop.
    
new exsits_number[33], num
    
for(new 1<= maxplayersi++)
    {
        if(
is_user_alive(i) && PlayerInfo[i] == HUMAN)
        {
            
num random_num(1alive_humans)

            while(
exsits_number[num] != 0)
            {
                
num random_num(1alive_humans)
            }
            
pnumber[i] = exsits_number[num] = num
        
}
    }    


EDIT: if u don't understand that now, how it works ask me :)

Exolent[jNr] 03-16-2011 06:12

Re: Random number to each player.
 
Better to use this method:

PHP Code:

set_numbers()
{
    new 
alive_humans get_alivehumans()
    
    
// fill numbers from 1 to alive_humans
    
new numbers[32]; // 32 since maximum for server is 32 players
    
for(new 0alive_humansi++)
    {
        
numbers[i] = 1;
    }
    
    for(new 
1j<= maxplayersi++)
    {
        if(
is_user_alive(i) && PlayerInfo[i] == HUMAN)
        {
            
// get random index from numbers array
            
random(alive_humans);
            
            
// store value for player
            
pnumber[i] = numbers[j];
            
            
// replace random number with the last number in the array
            // then reduce the amount of numbers left
            
numbers[j] = numbers[--alive_humans];
        }
    }


This way there are no unnecessary loops.

Paulster1022 03-16-2011 12:58

Re: Random number to each player.
 
Thanks for the help. Exolent, is there a reason why you used random instead of random_num? Does random only randomize specific values? While random_num randomizes a range of values?

For example random can randomize numbers like 1, 3, 5, 8.
random_num has to randomize numbers within the range of 1 - 8. So 2, 4, 6, 7 must be included. Not sure if i get this =P.

Emp` 03-16-2011 13:01

Re: Random number to each player.
 
http://www.amxmodx.org/funcwiki.php?go=func&id=41

schmurgel1983 03-16-2011 14:07

Re: Random number to each player.
 
use my
get_alivehumans()return maybe 8

so my sample
give player 1 the 4
give player 2 the 6
give player 3 the 1
give player 4 the 4 O.o huch 4 already used search for non-used number....got one...give player 4 the 7
etc etc :)

Exolent[jNr] 03-16-2011 16:49

Re: Random number to each player.
 
Quote:

Originally Posted by schmurgel1983 (Post 1434279)
use my
get_alivehumans()return maybe 8

so my sample
give player 1 the 4
give player 2 the 6
give player 3 the 1
give player 4 the 4 O.o huch 4 already used search for non-used number....got one...give player 4 the 7
etc etc :)

Yours is inefficient because you check if a number has been used instead of removing it from the list of available numbers.

Paulster1022 03-16-2011 18:13

Re: Random number to each player.
 
Exolent, thank you. Crashes are not happening anymore. I didn't understand your method at first, but eventually it made sense to me. Thank you schmurgel for helpin out too.


All times are GMT -4. The time now is 14:41.

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