[Solved] Exact random chance

Get help using Construct 2

» Fri Feb 14, 2014 3:00 am

I want a function to return the values 0, 1 or 2 based on a random chance. Let's say 10% 0, 30% 1, 60% 2. How can this be achieved?

What I would normally do is this:

on "function"
random(100) < 10 -> return 0
random(100) < 30 -> return 1
random(100) < 60 -> return 2

But in doing this, the first one in the list will get a significantly smaller chance of happening, as the last ones will replace the final value
Ex:
Ok, the 10% went through, and the return value is set to 0. But wait, the 60% chance also went through, so the return value is now 2. So this will cause the 10% to be much less than actually 10%

I tried using "else", but this has the opposite effect:

on "function"
random(100) < 10 -> return 0
else random(100) < 30 -> return 1
else random(100) < 60 -> return 2

So, the second chance will only fire assuming the first one failed, so that causes the second one to only even fire 90% of the time

EDIT: I think I've found a way... I'll test it and post here later7Soul2014-02-14 22:19:48
B
87
S
22
G
15
Posts: 1,041
Reputation: 15,343

» Fri Feb 14, 2014 3:48 am

variable rand=0

rand = random(100)
if(rand < 60)
return 2
if(rand < 30)
return 1
if(rand < 10)
return 0
B
17
S
8
G
4
Posts: 93
Reputation: 3,190

» Fri Feb 14, 2014 3:54 am

choose(0,1,1,1,2,2,2,2,2,2)
B
71
S
22
G
280
Posts: 3,842
Reputation: 154,386

» Fri Feb 14, 2014 4:57 am

@c4sp3r89 that's exactly what I said doesn't work
@blackhornet choose() is too limiting, it's useful only if the values are static, and I want to change the percentages chances

So, this is what I made

Basically I have an array with 100 width, and then I fill it with the values.

Using the example from the first post:

From 0 to 9 X it takes the value 0
From 10 to 29 X it takes the value 1
From 30 to 99 X it takes the value 2

Now I can pick a random X value from the array, and it is going to be precise 10%, 30% and 60% chances

Edit: actually I forgot something. The For "second" start index is supposed to be

max(func.Param(((loopIndex("first")-1)*2)+1),0) + max(func.Param(((loopIndex("first")-2)*2)+1),0)7Soul2014-02-14 05:03:56
B
87
S
22
G
15
Posts: 1,041
Reputation: 15,343

» Fri Feb 14, 2014 5:35 am

On your 1st post. You state that you need to random 10%,30%,60%.

on "function"
random(100) < 10 -> return 0
random(100) < 30 -> return 1
random(100) < 60 -> return 2

which will random(100) on each condition which will be something like:
55 < 10
20 < 30
80 < 60
which will already give the wrong random chance.

on "function"
random(100) < 10 -> return 0
else random(100) < 30 -> return 1
else random(100) < 60 -> return 2

will follow the same thing. Except that it won't run through all 3 condition when the previous condition is met.
30 < 10 (false, go to else)
12 < 30 (true, skip the rest)
It will basically give the wrong random chance also.

Another example to explain clearer:

I create a variable rand to store the random once.
variable rand=0
rand = random(100)

Using the same variable, check through all 3 conditions.
eg. rand=30

if(rand >= 0 && rand < 10) // 10%
return 1
if(rand >= 10 && rand < 40) // 30%
return 2
if(rand >=40 && rand < 100) // 60%
return 3

Does this give a clearer view?c4sp3r892014-02-14 05:37:11
B
17
S
8
G
4
Posts: 93
Reputation: 3,190

» Fri Feb 14, 2014 9:29 am

do it the other way round...

10,30 and 60% chances ?

call function with 10 and 30 as parameters

function picks a random number (X)

set function to return 3 (ie the random number is less than 100)

then if X < (Parm(1)+Parm(0) (ie x<40) set function to return 2

then if X<(param(0) (X<10) set function to return 1

and the capxRamPackWobble2014-02-14 09:55:40
August 2015 - I misplaced a lot of links - I will try to find backups and repost. If You find a post that interests please reply to post with @rampackwobble and I will get a nudge!

If you find my posts useful please consider donating something to a local charity or to the http://www.mndassociation.org/
B
64
S
34
G
47
Posts: 1,194
Reputation: 30,902

» Fri Feb 14, 2014 10:08 am

Why don't you simply populate your array with the desired values and then use n=floor(random(100))to select a random value from the array at n,1?Bertie Booster2014-02-14 10:12:58
There are 10 types of people in the World, those that understand binary and those who don't.
B
7
S
2
Posts: 93
Reputation: 797

» Fri Feb 14, 2014 10:56 am

Because of being quite sad I ran the above function 10,000 times and it returned

1-914 times (9%)
2-3070 times (31%)
3-6016 times (60%)

so it appears to work ok...
August 2015 - I misplaced a lot of links - I will try to find backups and repost. If You find a post that interests please reply to post with @rampackwobble and I will get a nudge!

If you find my posts useful please consider donating something to a local charity or to the http://www.mndassociation.org/
B
64
S
34
G
47
Posts: 1,194
Reputation: 30,902

» Fri Feb 14, 2014 10:22 pm

@RamPackWobble @c4sp3r89
Those are all valid solutions. I'm not used to using local variables so that didn't even come to my mind.
B
87
S
22
G
15
Posts: 1,041
Reputation: 15,343