Raised This Month: $ Target: $400
 0% 

Random number to each player.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Paulster1022
Member
Join Date: Apr 2006
Old 03-16-2011 , 04:09   Random number to each player.
Reply With Quote #1

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
}

Last edited by Paulster1022; 03-16-2011 at 04:13.
Paulster1022 is offline
Send a message via AIM to Paulster1022
schmurgel1983
Veteran Member
Join Date: Aug 2006
Location: Germany
Old 03-16-2011 , 04:36   Re: Random number to each player.
Reply With Quote #2

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
__________________

Working on:
nothing

Last edited by schmurgel1983; 03-16-2011 at 05:24.
schmurgel1983 is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 03-16-2011 , 06:12   Re: Random number to each player.
Reply With Quote #3

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.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Paulster1022
Member
Join Date: Apr 2006
Old 03-16-2011 , 12:58   Re: Random number to each player.
Reply With Quote #4

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.
Paulster1022 is offline
Send a message via AIM to Paulster1022
Emp`
AMX Mod X Plugin Approver
Join Date: Aug 2005
Location: Decapod 10
Old 03-16-2011 , 13:01   Re: Random number to each player.
Reply With Quote #5

http://www.amxmodx.org/funcwiki.php?go=func&id=41
Emp` is offline
Send a message via AIM to Emp` Send a message via MSN to Emp` Send a message via Yahoo to Emp` Send a message via Skype™ to Emp`
schmurgel1983
Veteran Member
Join Date: Aug 2006
Location: Germany
Old 03-16-2011 , 14:07   Re: Random number to each player.
Reply With Quote #6

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
__________________

Working on:
nothing
schmurgel1983 is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 03-16-2011 , 16:49   Re: Random number to each player.
Reply With Quote #7

Quote:
Originally Posted by schmurgel1983 View Post
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.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Paulster1022
Member
Join Date: Apr 2006
Old 03-16-2011 , 18:13   Re: Random number to each player.
Reply With Quote #8

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.
Paulster1022 is offline
Send a message via AIM to Paulster1022
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 14:41.


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