Random Functions?

Posted By: gamers

Random Functions? - 03/27/20 10:56

Hi guys,
There are two different functions in my project that I want to use. How do I get these two different functions to be displayed to the user 15 times and randomly.
So I would like to present a set of these two functions to users 15 times.
For example, one function is A and the other is B. I want to present the A and B functions to the user randomly 15 times.
How can I do that (i.e, "A, A, B, B, B, A, B, A, B, A, A, A, B, B, A" - OR - "B, A, B, A, B, B, B, A, A, A, B, A, B, B, B")
Thanks for your support.
Regards,
Posted By: txesmi

Re: Random Functions? - 03/27/20 12:28

Hi,
something like this?
Code
var _i = 0;
for(; _i < 15; _i += 1) {
   if(random(1) < 0.5)
      A();
   else
      B();
}
Posted By: gamers

Re: Random Functions? - 03/29/20 07:59

Thank you very much @txesmi, I will try this..

Originally Posted by txesmi
Hi,
something like this?
Code
var _i = 0;
for(; _i < 15; _i += 1) {
   if(random(1) < 0.5)
      A();
   else
      B();
}
Posted By: gamers

Re: Random Functions? - 03/31/20 12:08

I tried using different functions by assigning this code to a key (i.e., space), but I can use it more than 15 times. Is there another way to limit it to 15?
Posted By: Emre

Re: Random Functions? - 03/31/20 12:32

You should declare the variable "_i" as a global variable.

Code

var _i=0; //global var

function on_space_event()
{
	for(; _i < 15; _i += 1) {
		if(random(1) < 0.5)
		A();
		else
		B();
	}
}


and if you want just one function to work each time you press the key, you don't need to use "for" loop. Something like this works:

Code
var _i=0;

function on_space_event()
{
	_i+=1;
	if( _i>15)
	{
		return;
	}

	if(random(1) < 0.5)
	A();
	else
	B();
}
}
Posted By: gamers

Re: Random Functions? - 04/02/20 09:01

Originally Posted by Emre
You should declare the variable "_i" as a global variable.

Code

var _i=0; //global var

function on_space_event()
{
	for(; _i < 15; _i += 1) {
		if(random(1) < 0.5)
		A();
		else
		B();
	}
}


and if you want just one function to work each time you press the key, you don't need to use "for" loop. Something like this works:

Code
var _i=0;

function on_space_event()
{
	_i+=1;
	if( _i>15)
	{
		return;
	}

	if(random(1) < 0.5)
	A();
	else
	B();
}
}


Thank you @Emre, I think that's exactly what I want!
© 2024 lite-C Forums