AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   random_num(0,1) is ALWAYS 1 (https://forums.alliedmods.net/showthread.php?t=155003)

fmfs10 04-16-2011 10:45

random_num(0,1) is ALWAYS 1
 
Hello! I've made a deathrun plugin for my server based on Deathrun Manager by xPaw

I did a function that happens in every new round, that gives 1 life to a random players, but giving a bigger chance to vips from my server to have this random life. Here is the code:

PHP Code:

// Random life to a player
stock random_life() {
    
    new 
bool:PlayerChecked
    
    
// Do a loop until it gets a player...
    
while(!PlayerChecked) {
        
        new 
iPlayers[32], Players
        get_players
iPlayersPlayers"c" );
        
        if(!
Players)     break
        
        new 
only_vips random_num(01)
        
        if(
only_vips) {
            
            new 
vip_id[33]
            new 
vip_slot = -1
            
            
for(new iPlayersi++) {
                
                if(
get_user_flags(iPlayers[i]) & ADMIN_RESERVATION) {
                    
vip_id[vip_slot+1] = iPlayers[i]
                    
vip_slot++
                }
            }
            
            if(
vip_slot != -1) {
                
                new 
vip_choosen random_num(0vip_slot+1)
                
                if(
is_user_connected(vip_id[vip_choosen])) {
                    
                    
g_Lifes[vip_id[vip_choosen]]++
                    
                    new 
name[64]
                    
get_user_name(vip_id[vip_choosen], name63)
                    
                    
set_hudmessage012800.040.7102.52.50.50.5);
                    
                    
// It shows (VIP) just for check if it was only_vips
                    // And it ALWAYS appear... lol
                    
ShowSyncHudMsgvip_id[vip_choosen], VidasHudSync2"%s ganhou uma vida extra! (VIP)"name);
                    
                    
PlayerChecked true
                
}
            }    
        }
        else {
            
            new 
Random_Player random_num(0Players-1)
            
            if(
is_user_connected(Random_Player)) {
                
                
g_Lifes[Random_Player]++
                
                new 
name[64]
                
get_user_name(Random_Playername63)
                
                
set_hudmessage012800.040.7102.52.50.50.5);
                
                
ShowSyncHudMsgRandom_PlayerVidasHudSync2"%s ganhou uma vida extra!"name);
                
                
PlayerChecked true
            
}
        }
    }
    


What's wrong with my code?
P.S.: I'm testing alone on my server...

Bugsy 04-16-2011 11:00

Re: random_num(0,1) is ALWAYS 1
 
I haven't evaluated your code closely (Im on my phone typing) but keep in mind its a 50/50 shot of the value being 1. How many times have you tested?

Also, you are using && for checking flags, should only be & since its a bit-comparison. There are other improvements that can be made.

fmfs10 04-16-2011 11:03

Re: random_num(0,1) is ALWAYS 1
 
I know... But a tried a lot of times ( like 20 times) and I always receive the (VIP) message...

About &&, my fault, fixed :P

I know that it's possible to have some improvements, but didn't think in a better way... '-'

Bugsy 04-16-2011 11:28

Re: random_num(0,1) is ALWAYS 1
 
If nobody else chimes in I will take care of it later for you. In the meantime, try debugging yourself. Print out values in your code to see where the error is occurring.

ConnorMcLeod 04-16-2011 11:48

Re: random_num(0,1) is ALWAYS 1
 
PHP Code:

    new iRandomiRandomNum
    
for(new ii<10000i++)
    {
        
iRandom += random(2)
        
iRandomNum += random_num(0,1)
    }

    
server_print("random %d, random_num %d"iRandomiRandomNum

Code:

random 4995, random_num 5037
seems fair enough

fmfs10 04-16-2011 12:08

Re: random_num(0,1) is ALWAYS 1
 
EDIT@
Didn't seem the above message :P
Anyways, I think it doesn't fix my problem :x


I did a check and see what it printed:

PHP Code:

TRUE
PLAYER NOT SELECTED
FALSE
PLAYER NOT SELECTED
FALSE
PLAYER NOT SELECTED
TRUE
PLAYER SELECTED 
MODETRUE 

CODE:

PHP Code:

// Random life to a player
stock random_life() {
    
    new 
bool:PlayerChecked
    
    
// Do a loop until it gets a player...
    
while(!PlayerChecked) {
        
        new 
iPlayers[32], Players
        get_players
iPlayersPlayers"c" );
        
        if(!
Players)     break
        
        new 
only_vips random_num(01)
        
        if(
only_vips) {
            
            
client_print(0print_chat"TRUE")
            
            new 
vip_id[33]
            new 
vip_slot = -1
            
            
for(new iPlayersi++) {
                
                if(
get_user_flags(iPlayers[i]) & ADMIN_RESERVATION) {
                    
vip_id[vip_slot+1] = iPlayers[i]
                    
vip_slot++
                }
            }
            
            if(
vip_slot != -1) {
                
                new 
vip_choosen random_num(0vip_slot+1)
                
                if(
is_user_connected(vip_id[vip_choosen])) {
                    
                    
g_Lifes[vip_id[vip_choosen]]++
                    
                    new 
name[64]
                    
get_user_name(vip_id[vip_choosen], name63)
                    
                    
set_hudmessage012800.040.7102.52.50.50.5);
                    
                    
// It shows (VIP) just for check if it was only_vips
                    // And it ALWAYS appear... lol
                    
ShowSyncHudMsgvip_id[vip_choosen], VidasHudSync2"%s ganhou uma vida extra! (VIP)"name);
                    
                    
PlayerChecked true
                
}
            }    
        }
        else {
            
            
client_print(0print_chat"FALSE")
            
            new 
Random_Player random_num(0Players-1)
            
            if(
is_user_connected(Random_Player)) {
                
                
g_Lifes[Random_Player]++
                
                new 
name[64]
                
get_user_name(Random_Playername63)
                
                
set_hudmessage012800.040.7102.52.50.50.5);
                
                
ShowSyncHudMsgRandom_PlayerVidasHudSync2"%s ganhou uma vida extra!"name);
                
                
PlayerChecked true
            
}
        }
        
        if(!
PlayerChecked)
            
client_print(0print_chat"PLAYER NOT SELECTED")
        else
            
client_print(0print_chat"PLAYER SELECTED - MODE: %s"only_vips "TRUE" "FALSE")
    }
    


It's like it's not finding the player in FALSE, but sometimes not in TRUE too...

nikhilgupta345 04-16-2011 12:19

Re: random_num(0,1) is ALWAYS 1
 

PHP Code:

if(is_user_connected(Random_Player)) 

->

PHP Code:

if( is_user_connectediPlayers[Random_Player] ) ) 


fmfs10 04-16-2011 12:57

Re: random_num(0,1) is ALWAYS 1
 
Oh, now it fixed my problems! Thanks!

PHP Code:

random_life() {
    
    new 
bool:PlayerChecked
    
    
while(!PlayerChecked) {
        
        new 
iPlayers[32], Players
        get_players
iPlayersPlayers"c" );
        
        if(!
Players)     break
        
        new 
only_vips random_num(01)
        
        if(
only_vips) {
            
            new 
vip_id[33]
            new 
vip_slot = -1
            
            
for(new iPlayersi++) {
                if(
user_vip[iPlayers[i]]) {
                    
vip_id[vip_slot+1] = iPlayers[i]
                    
vip_slot++
                }
            }
            
            if(
vip_slot != -1) {
                
                new 
vip_choosen random_num(0vip_slot)
                
                if(
is_user_connected(vip_id[vip_choosen])) {
                    
                    
g_Lifes[vip_id[vip_choosen]]++
                    
                    new 
name[64]
                    
get_user_name(vip_id[vip_choosen], name63)
                    
                    
set_hudmessage012800.040.7102.52.50.50.5);
                    
                    
ShowSyncHudMsg0VidasHudSync2"%s ganhou uma vida extra!"name);
                    
                    
PlayerChecked true
                
}            
            }
        }
        else {
            
            new 
Random_Player random_num(0Players-1)
            
            if(
is_user_connected(iPlayers[Random_Player])) {
                
                
g_Lifes[iPlayers[Random_Player]]++
                
                new 
name[64]
                
get_user_name(iPlayers[Random_Player], name63)
                
                
set_hudmessage012800.040.7102.52.50.50.5);
                
                
ShowSyncHudMsg0VidasHudSync2"%s ganhou uma vida extra!"name);
                
                
PlayerChecked true
            
}
        }
    }
    


But if there is some optimization for my code, I would appreciate that :P


All times are GMT -4. The time now is 20:11.

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