Raised This Month: $ Target: $400
 0% 

random_num with exceptions?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Silencer123
Veteran Member
Join Date: Jul 2006
Old 04-10-2007 , 09:26   random_num with exceptions?
Reply With Quote #1

I searched all over the forum and the documentation/wiki of AMXX,
but did not find anything that could answer this question. Nevertheless
for the case that this has been asked before, I am sorry.

My question is, if it is possible to make the random_num function handle
exceptions. For example, I want a random number between 0 and 1000.
Code:
random_num(0,1000)
Now I, for example, do not want to get the values 3, 55, 76, 102, 123, 666 and 998.
What would I do in order to have these exceptions? Note that these numbers
shall be able to vary and that I do not want to run the same function again and
again until an acceptable result has been generated. However, that would look
like this, but I do not think that it is good:
Code:
public random_0_1000(ex_a,ex_b,ex_c,ex_d,ex_e,ex_f,ex_g) {     new num=random_num(0,1000)     if(num!=ex_a&&num!=ex_b&&num!=ex_c&&num!=ex_d&&num!=ex_e&&num!=ex_f&&num!=ex_g)     {         return num     }     else     {         random_0_1000()     } }
If something like the following would work, I would be happy.
Code:
random_num(0,1000,!ex_a,!ex_b,!ex_c,!ex_d,!ex_e,!ex_f,!ex_g)
__________________
EAT YOUR VEGGIES

Last edited by Silencer123; 04-10-2007 at 09:30.
Silencer123 is offline
Brad
AMX Mod X Team Member
Join Date: Jun 2004
Old 04-10-2007 , 10:23   Re: random_num with exceptions?
Reply With Quote #2

You would need to do something like your random_0_1000 function.

You might want to allow 0 or more excluded numbers instead of hardcoding it to 7. You'll also need to return random_0_1000() in your else statement.
__________________
Brad is offline
jim_yang
Veteran Member
Join Date: Aug 2006
Old 04-10-2007 , 12:07   Re: random_num with exceptions?
Reply With Quote #3

Code:
#include <amxmodx> #include <amxmisc> #define PLUGIN "Test" #define VERSION "1.0" #define AUTHOR "Jim" new testarray[10] = {1,2,3,4,5,6,7,8,9,10} public plugin_init() {         register_plugin(PLUGIN, VERSION, AUTHOR)         register_clcmd("test","test") } public test(id) {         client_print(id, print_console, "result: %d", random_num_exp(1, 11, testarray, 10)) } public random_num_exp(_min, _max, const exp[], num) {         new rnd         new tmp         new bool:loop = true         while(loop)         {                 rnd = random_num(_min, _max)                 for(new i; i < num; i++)                 {                         if(exp[i] == rnd)                         {                                 tmp = 0                                 break                         }                         else                         {                                 tmp++                         }                 }                 if(tmp == num)                         loop = false         }         return rnd }
__________________
Project : CSDM all in one - 99%
<team balancer#no round end#entity remover#quake sounds#fake full#maps management menu#players punishment menu#no team flash#colored flashbang#grenade trails#HE effect#spawn protection#weapon arena#weapon upgrade#auto join#no weapon drop#one name>
jim_yang is offline
Silencer123
Veteran Member
Join Date: Jul 2006
Old 04-10-2007 , 17:13   Re: random_num with exceptions?
Reply With Quote #4

"const exp[]" // What does "const" mean/do ?
"break" // What does this do ?
It would be nice if you could give some additional explaination on how
your code works, because I am actually getting a headache when trying
to understand that in a minute.
__________________
EAT YOUR VEGGIES
Silencer123 is offline
Emp`
AMX Mod X Plugin Approver
Join Date: Aug 2005
Location: Decapod 10
Old 04-10-2007 , 17:41   Re: random_num with exceptions?
Reply With Quote #5

const - value can't be changed after initialization. if parameter, the function cannot change it.

break - stops a loop from continuing

Code:
new num = random_num(0, 1000);
while( num == a || num == b  || num == c) //etc.
   num = random_num(0, 1000);
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`
Silencer123
Veteran Member
Join Date: Jul 2006
Old 04-10-2007 , 19:07   Re: random_num with exceptions?
Reply With Quote #6

Uuuh... Thanks for the Information!
Well I found a better way to fix the main problem I had than a random_num with exceptions.
Thanks to all of you anyways, have some Karma.
__________________
EAT YOUR VEGGIES
Silencer123 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 06:44.


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