Raised This Month: $ Target: $400
 0% 

other way of while loop?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Crusher918
Senior Member
Join Date: Feb 2007
Location: New York
Old 05-23-2007 , 20:41   other way of while loop?
Reply With Quote #1

I was trying to make something for my plugin that has this

PHP Code:
#include <amxmodx>
 
#define PLUGIN "while loop"
#define VERSION "1.0"
#define AUTHOR "Crusher"
 
new x[10]
 
public 
plugin_init() {
 
register_plugin(PLUGINVERSIONAUTHOR)
 
register_plugin("say /test","cmd_test")
}
public 
cmd_test(id) {
 new 
number 
 
 
while(x[number] != 1) {
 
  
number random_num(1,10)
 
 }
 
x[number] = 1
 
 client_print
(id,print_chat,"Test number: %d",number)
 return 
PLUGIN_HANDLED

the problem is the 9th or 10th time using the command Sometimes the number returns as 0

Does anyone knows of any other way to do this?
__________________
My Plugins
Newest plugin: semiclip , amx_bankrupt

Last edited by Crusher918; 05-23-2007 at 23:37.
Crusher918 is offline
Cheap_Suit
Veteran Member
Join Date: May 2004
Old 05-23-2007 , 21:08   Re: other way of while loop?
Reply With Quote #2

PHP Code:
new x[11
__________________
HDD fried, failed to backup files. Sorry folks, just don't have free time anymore. This is goodbye.
Cheap_Suit is offline
Crusher918
Senior Member
Join Date: Feb 2007
Location: New York
Old 05-23-2007 , 23:35   Re: other way of while loop?
Reply With Quote #3

Quote:
Originally Posted by Cheap_Suit View Post
PHP Code:
new x[11
but i want all the numbers to be used

and making it like that would give the 10th time to 0
__________________
My Plugins
Newest plugin: semiclip , amx_bankrupt
Crusher918 is offline
Cheap_Suit
Veteran Member
Join Date: May 2004
Old 05-23-2007 , 23:56   Re: other way of while loop?
Reply With Quote #4

You can only store 10 numbers with an array[10] (0-9)
So by increasing the array to 11 you'll able to use 0 - 10,
or 1-10 in your case.
__________________
HDD fried, failed to backup files. Sorry folks, just don't have free time anymore. This is goodbye.

Last edited by Cheap_Suit; 05-24-2007 at 02:36. Reason: oops
Cheap_Suit is offline
Crusher918
Senior Member
Join Date: Feb 2007
Location: New York
Old 05-24-2007 , 00:44   Re: other way of while loop?
Reply With Quote #5

Quote:
Originally Posted by Cheap_Suit View Post
You can only store 9 numbers with an array[10] (0-9)
So by increasing the array to 11 you'll able to use 0 - 10,
or 1-10 in your case.
i know that but wat i meant was i want all the array to be used
because i want the users to use the cmd so each person gets a number and they all have to be different numbers
__________________
My Plugins
Newest plugin: semiclip , amx_bankrupt

Last edited by Crusher918; 05-24-2007 at 00:46.
Crusher918 is offline
regalis
Veteran Member
Join Date: Jan 2007
Location: F*cking Germany
Old 05-24-2007 , 04:49   Re: other way of while loop?
Reply With Quote #6

Quote:
Originally Posted by Crusher918 View Post
I was trying to make something for my plugin that has this

PHP Code:
#include <amxmodx>
 
#define PLUGIN "while loop"
#define VERSION "1.0"
#define AUTHOR "Crusher"
 
new x[10]
 
public 
plugin_init() {
 
register_plugin(PLUGINVERSIONAUTHOR)
 
register_plugin("say /test","cmd_test")
}
public 
cmd_test(id) {
 new 
number 
 
 
while(x[number] != 1) {
 
  
number random_num(1,10)
 
 }
 
x[number] = 1
 
 client_print
(id,print_chat,"Test number: %d",number)
 return 
PLUGIN_HANDLED

the problem is the 9th or 10th time using the command Sometimes the number returns as 0

Does anyone knows of any other way to do this?
I think x[number] will be everytime = 0 !
Because you initialize the array and don't assign numbers...
In your case:
x[0] = 0
x[1] = 0
x[2] = 0
x[3] = 0
....and so on

You should do something like that

new x[] = {1,2,3,4,5,6,7,8,9,10}

That should do the trick..for all numbers to be different you have to work out a function which stores the numbers given, and compare every new number with all the given ones...
I'm a little tired maybe i'll post later a function when my coffee wake up my
brain*lol*

greetz regalis
__________________
regalis is offline
_Master_
Senior Member
Join Date: Dec 2006
Old 05-24-2007 , 13:37   Re: other way of while loop?
Reply With Quote #7

Quote:
Originally Posted by regalis View Post
I think x[number] will be everytime = 0 !
That's good bacause the test is != 1 (0 means the number hasn't been assigned to a player, 1 means it has - so he must peek another)

@ Crusher918 : use Cheap_Suit's hotfix . Also the is another thing you should consider: if random_num() returns 3 in a loop of 100 it will take a while to execute that command. Also random_num() seldom returns the max allowed value, so if that value is the only one left valid it will also take a while to do it.

Last edited by _Master_; 05-24-2007 at 13:43.
_Master_ is offline
Old 05-24-2007, 13:42
_Master_
This message has been deleted by _Master_. Reason: duplicate post
Crusher918
Senior Member
Join Date: Feb 2007
Location: New York
Old 05-24-2007 , 17:11   Re: other way of while loop?
Reply With Quote #8

Quote:
Originally Posted by _Master_ View Post
That's good bacause the test is != 1 (0 means the number hasn't been assigned to a player, 1 means it has - so he must peek another)

@ Crusher918 : use Cheap_Suit's hotfix . Also the is another thing you should consider: if random_num() returns 3 in a loop of 100 it will take a while to execute that command. Also random_num() seldom returns the max allowed value, so if that value is the only one left valid it will also take a while to do it.
yes that wat im trying to do

and for the random_num()
-Sometimes the last number would return to 0 due to reaching the max value
well is there any other way to do it so if its one left it will choose the last number?
__________________
My Plugins
Newest plugin: semiclip , amx_bankrupt

Last edited by Crusher918; 05-24-2007 at 17:21.
Crusher918 is offline
_Master_
Senior Member
Join Date: Dec 2006
Old 05-25-2007 , 01:36   Re: other way of while loop?
Reply With Quote #9

There are 2 obvious ways you can do this:
1. extend the max value to 15 for instance. Thus you will have 5 possible values above the max allowed (15 might be a bit much but it will do the job). It will look like this:
PHP Code:
public cmd_test(id){
    
// number must be initialized because, in this case, x[0] will always be != 1
    
new number 1
    
    
while(x[number] != 1){
        
number random_num(115)
        if(
number 10number 10
    
}
    
    
x[number] = 1
    
    
//
    // ADD STUFF HERE
    //
    
    
return PLUGIN_HANDLED

2. This method implies that the number is rather incremental (ie NOT random) !!! You can re-organize the way the index is used: At the moment x[i] stores the VALUE assigned to the player index (refferrenced by "i"). Instead of this you can store the player INDEX in x[i], "i" means the number assigned to. This way you will have a "time-finite" method and you won't have to worry about random_num(). The code will look like this:
PHP Code:
public cmd_test(id){
    new 
i
    
    
// x[i] holds the player index so if it's == 0 then it hasn't been used
    // In this case stop looking any further.
    
for(111i++) if(!x[i]) break
        
    
x[i] = id
    
    
//
    // ADD STUFF HERE
    //
    
    
return PLUGIN_HANDLED


There are other methods you can use. For the purpose of this you could write a custom_random_num() function by yourself and handle those drawbacks in std random_num(); you could use Mel's RandomX module (also a bit too much ), etc.

Last edited by _Master_; 05-25-2007 at 01:41.
_Master_ is offline
kmal2t
BANNED
Join Date: Apr 2006
Old 05-25-2007 , 04:23   Re: other way of while loop?
Reply With Quote #10

I think I know what you mean and I did something like this the other day with 2 for loops to have one give numbers and the other check the numbers for repeats.

Personally I'd like to see a function that could assign numbers to arrays for a random number set. I.e if you have an array[10] those array[0] through array[9] would each be given randomly one of the numbers between 1-10 so that none of the arrays have the same number like:
new array[6]
command(array)
then with a get_command(array) you would know that:
array[0] = 3
array[1] = 5
array[2] = 6
array[3] = 2
array[4] = 4
array[5] = 1
etc. etc.

You could use this to easily shufle players to different groups or give each player unique things like every player has one random individual weapon or characteristic without having to check if a number has already been used.

Last edited by kmal2t; 05-25-2007 at 04:25.
kmal2t 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 10:31.


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