Raised This Month: $ Target: $400
 0% 

random_num(0,1) is ALWAYS 1


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
fmfs10
Senior Member
Join Date: Mar 2009
Location: Brasil
Old 04-16-2011 , 10:45   random_num(0,1) is ALWAYS 1
Reply With Quote #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...

Last edited by fmfs10; 04-16-2011 at 11:03.
fmfs10 is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-16-2011 , 11:00   Re: random_num(0,1) is ALWAYS 1
Reply With Quote #2

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.
__________________

Last edited by Bugsy; 04-16-2011 at 11:03.
Bugsy is offline
fmfs10
Senior Member
Join Date: Mar 2009
Location: Brasil
Old 04-16-2011 , 11:03   Re: random_num(0,1) is ALWAYS 1
Reply With Quote #3

I know... But a tried a lot of times ( like 20 times) and I always receive the (VIP) message...

About &&, my fault, fixed

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

Last edited by fmfs10; 04-16-2011 at 11:06.
fmfs10 is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-16-2011 , 11:28   Re: random_num(0,1) is ALWAYS 1
Reply With Quote #4

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.
__________________
Bugsy is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 04-16-2011 , 11:48   Re: random_num(0,1) is ALWAYS 1
Reply With Quote #5

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
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
fmfs10
Senior Member
Join Date: Mar 2009
Location: Brasil
Old 04-16-2011 , 12:08   Re: random_num(0,1) is ALWAYS 1
Reply With Quote #6

EDIT@
Didn't seem the above message
Anyways, I think it doesn't fix my problem


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...

Last edited by fmfs10; 04-16-2011 at 12:13.
fmfs10 is offline
nikhilgupta345
Veteran Member
Join Date: Aug 2009
Location: Virginia
Old 04-16-2011 , 12:19   Re: random_num(0,1) is ALWAYS 1
Reply With Quote #7


PHP Code:
if(is_user_connected(Random_Player)) 
->

PHP Code:
if( is_user_connectediPlayers[Random_Player] ) ) 
__________________
Quote:
Originally Posted by DarkGod View Post
nikhilgupta generates his plugins using sheer awesome.
If you like my work, please
nikhilgupta345 is offline
Send a message via ICQ to nikhilgupta345 Send a message via Yahoo to nikhilgupta345
fmfs10
Senior Member
Join Date: Mar 2009
Location: Brasil
Old 04-16-2011 , 12:57   Re: random_num(0,1) is ALWAYS 1
Reply With Quote #8

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
fmfs10 is offline
Reply


Thread Tools
Display Modes

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 20:11.


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