pick a random function.

Posted By: Realspawn

pick a random function. - 10/11/12 19:56

I have made several functions. say function 1 2 3
How can i activate a random function on a key press ?

thx for you time laugh
Posted By: WretchedSid

Re: pick a random function. - 10/11/12 19:57

random(), random_seed(), switch/case
Posted By: Kartoffel

Re: pick a random function. - 10/11/12 19:58

you can use function-pointer-arrays and use a random integer as index number when calling it.

I'm sorry, I'm not sure how it exactly works but it should.

EDIT: JustSid was faster, and his solution might be the better one wink
Posted By: Realspawn

Re: pick a random function. - 10/11/12 20:17

justsids answer in the words i allready found but not really know how to use them yet wink
Posted By: Aquilis

Re: pick a random function. - 10/11/12 20:19

Code:
function execute_random()
{
var execute=integer(random(3));
switch (execute)
{
case 0:
function_1();
break;
case 1:
function_2();
break;
case 2:
function_3();
break;
}
}

on_f=execute_random;


Posted By: Espér

Re: pick a random function. - 10/11/12 20:21

Code:
var randofunc = 0;

....

randofunc = random(3);
if(randofunc > 2)
{function_a();}
else if(randofunc > 1)
{function_b();}
if(randofunc < 1)
{function_c();}

Posted By: Kartoffel

Re: pick a random function. - 10/11/12 20:23

@ Aquilis

will this work? because if execute == 1.534 case 1 won't work, will it?

don't you have to use switch(integer(execute(3) + 1)) to have a random integer between 1 and 3?
Posted By: Aquilis

Re: pick a random function. - 10/11/12 20:27

@Kartoffel, yes you are right. It should be var execute=integer(random(3));

I changed my post above. It should work fine now.
Posted By: Realspawn

Re: pick a random function. - 10/11/12 21:34

Aquilis you rock laugh thank you for this laugh

i'll make sure it will be used in future workshops for my site laugh
Posted By: krial057

Re: pick a random function. - 10/11/12 22:58

Other solution if functions are indexed(probably not as effective, but easier to implement(no need to add functions to a switch)

Code:
void quest_1();
void quest_2();
void quest_3();

void execute_random(STRING* funcprefix, int count)
{
	void myfunc();
	if(myfunc = engine_getscript(_chr(str_cat(_str(funcprefix),str_for_num(NULL, (int)random(count)+1)))))
		myfunc();
	else
	 printf("There is missing an indexed function...");
}

int main()
{
	random_seed(0);
	execute_random("quest_", 3);
	return 0;
}



(sorry, was in a compressive mood :P)
Posted By: EvilSOB

Re: pick a random function. - 10/12/12 09:34

Also a section of Aquilis's code could be 'optimised'...
Code:
...
   var execute=integer(random(3));
   switch(execute)
...

could be truncated down to
Code:
...
   switch(integer(random(3)))
...



I dont think my code is actually any "faster",
but it is less code... thats all. I just like less code...
Posted By: WretchedSid

Re: pick a random function. - 10/13/12 16:33

It shouldn't be faster because the compiler should do both versions directly with the eax register and not move the value around in a temporary register (but even if it did, moving values between registers is blazing fast on CPUs)
© 2024 lite-C Forums