AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   random_num with exceptions? (https://forums.alliedmods.net/showthread.php?t=53740)

Silencer123 04-10-2007 09:26

random_num with exceptions?
 
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)

Brad 04-10-2007 10:23

Re: random_num with exceptions?
 
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.

jim_yang 04-10-2007 12:07

Re: random_num with exceptions?
 
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 }

Silencer123 04-10-2007 17:13

Re: random_num with exceptions?
 
"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.

Emp` 04-10-2007 17:41

Re: random_num with exceptions?
 
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);


Silencer123 04-10-2007 19:07

Re: random_num with exceptions?
 
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.


All times are GMT -4. The time now is 06:44.

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