[Solved] Exact random chance

0 favourites
  • 9 posts
From the Asset Store
Easily generate many levels from a set of pre-built scenes (Construct 3 template)
  • 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 later

  • variable rand=0

    rand = random(100)

    if(rand < 60)

    return 2

    if(rand < 30)

    return 1

    if(rand < 10)

    return 0

  • choose(0,1,1,1,2,2,2,2,2,2)

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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

    <img src="http://i.imgur.com/tkbQVxI.png" border="0" />

    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)

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

    Your 1st example:

    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.

    Your 2nd example:

    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?

  • 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

    <img src="https://dl.dropboxusercontent.com/u/143636437/examples%20for%20web/Ashampoo_Snap_2014.02.14_09h47m47s_005.png" border="0">

    and the capx

  • 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?

  • 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...

  • RamPackWobble c4sp3r89

    Those are all valid solutions. I'm not used to using local variables so that didn't even come to my mind.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)