How to use random() efficiently

Get help using Construct 2

Post » Thu Nov 05, 2015 5:19 pm

Normally I use "choose()" but now I've to use "random()" because it will attend me in several layouts (eg: int(random(number, var))). The problem with random is the constant repetition of numbers. When I use "choose()", I usually include copies of the numbers or strings (eg: 1,2,3,4,5,5,4,3,2,1,1,3,5,2,4) and the possibility of repeating numbers is not very common. But now I need to use "random()" and would like to know if anyone have a simple solution to improve it.

Thanks in advance
B
74
S
14
G
4
Posts: 1,018
Reputation: 8,139

Post » Thu Nov 05, 2015 5:25 pm

I'm not sure what you mean..
int(random(number, var) just chooses a random number between your number and your var not including var

random(a, b)
Generate a random float between a and b, including a but not including b.
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Fri Nov 06, 2015 2:42 pm

Is the same thing that you wrote with the difference that I use a variable to define the random range. The problem of the "random()" is the repetition of the same number more frequently than "choose()", at least was that I've observed (eg number = random(0,5) > number = 5, number =5, number =3, number =5, ...). So, to prevent repetition when I use "choose()" I usually include several copies of these numbers in different positions like this: chosse(1,2,3,2,3,1,3,1,2). This works for me.
B
74
S
14
G
4
Posts: 1,018
Reputation: 8,139

Post » Fri Nov 06, 2015 2:52 pm

It shouldn't make any difference..

Choose(1,2,3)
is the same as
Choose(1,1,2,2,3,3)
is the same as
Choose(1,3,1,2,2,3)

Random(0,5) will give any number between 0 and 4.999999
if you are going to round it,
0 will only be if the number is between 0 and 0.499999),
but 1 will be between (0.5 and 1.49999)
So yes there is twice as much chance of 1 than there is of 0

I would use floor(random(0,variable+1) that way all numbers from 0 to variable have the same chance of being selected..

0 (from 0 to 0.99999)
variable(from variable to variable+0.99999)
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Fri Nov 06, 2015 3:15 pm

Thanks @LittleStain

I'll give it a try.
B
74
S
14
G
4
Posts: 1,018
Reputation: 8,139

Post » Fri Nov 06, 2015 3:37 pm

I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Fri Nov 06, 2015 4:03 pm

I use the following:

"floor(random(x+1))" if you need a range between 0 and X.
OR
"floor(random(b-a+1))+a" if you need a range between A and B.
ImageImageImage
B
31
S
19
G
82
Posts: 1,039
Reputation: 46,207

Post » Fri Nov 06, 2015 4:34 pm

@LittleStain - Thanks for the example.

@BackendFreak - Nice solutions. I'll try it.
B
74
S
14
G
4
Posts: 1,018
Reputation: 8,139


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 7 guests