# How do I make something happen by chance?

Get help using Construct 2

### » Sun Apr 22, 2012 6:34 am

Hard to explain, so I'll start by explaining what I would like to do. I am trying to find a method of calculating a chance for something to happen after an event happens. So, for example, when the player kills a Monster he has a certain percent chance that this monster will drop(spawn) some ammo.

Now, I've found a way to do it, but I wonder is there's a better one?
I made a variable called RandomNumber

Then I had an action during the the monsters "death" calculate a random number up too 100for the RandomeNumber variable and the following sub event would find a value between 50 and 100 in it. If it was between those numbers then it would spawn the ammo.

Seems like a solid solution and I theorize 50-100 of 100 is 50% drop rate. In turn, I have a 50% chance of my monster dropping the ammo. Or, for any number, if you choose any numbers between a certain number that is a percent of that number it will be that percent chance to do what you want. If that makes sense. Sorry.

Anyway, just looking for some input. I did search for this and I apologize in advance if a related topic already exists.DiGiCORE2012-04-22 06:53:07
- Diji
B
24
S
4
G
4
Posts: 54
Reputation: 4,043

### » Sun Apr 22, 2012 7:02 am

This seem like a working solution.
Just a note, use int(random(101)) in the top event.
This will only provide integers, and it will return 100 eventually.

Also as subevent you can only test for "RandomNumber >= 50".
New to Construct ? Where to start

Please attach a capx to any help request or bug report !
Moderator
B
306
S
126
G
99
Posts: 7,380
Reputation: 73,836

### » Sun Apr 22, 2012 7:07 am

Sounds great, thank's Kyatric. Much appreciated.
- Diji
B
24
S
4
G
4
Posts: 54
Reputation: 4,043

### » Sun Apr 22, 2012 8:57 am

Why not just use random(1,100) instead of random(100)? and i think what you're saying "50-100 of 100 is 50% drop rate", 50-100=50? it means is 1 out of 50 chances to get a drop, not 50% drop, random(1,2) would mean 50% chances. but using random(1,100) would be nice, if you're doing a game with different difficulties, you could reduce the numbers between the random method to make it easier to get a drop.
B
7
S
1
Posts: 5
Reputation: 573

### » Sun Apr 22, 2012 9:43 am

@andrefabud: the trick is in the following event since there's a test to see if the number is superior to 50, than you drop loot.
Meaning you have 50 chances out of 100 to have a drop => 50% chances.

Your reasoning is (almost) valid too.
With the random expression, the last number you give in the range will never be returned. So in your "random(1,2)" you could have results returned like 0.5 or 1.999999 or 0.122355565, etc...
That's where the int() cast is useful.
And considering you would use int(random(1,2)) then the only result would be 1.
With int(random(1,3)) you would either get 1 or 2, and then could, in the next event just test if the result is indeed 2 and then drop loot.
New to Construct ? Where to start

Please attach a capx to any help request or bug report !
Moderator
B
306
S
126
G
99
Posts: 7,380
Reputation: 73,836

### » Mon Apr 23, 2012 12:21 am

Alright, so, int(random(1,3)) only gives me 1 and 2 or 50% when selecting 1 or 2 and int(random(1,5) would give me 1-4 being around 25% when selecting one number between 1-4? Trying to understand exactly.

Also, when using, lets say, int(random(1,3). Why isn't 3 used? Not that it changes much, just curious.
- Diji
B
24
S
4
G
4
Posts: 54
Reputation: 4,043

### » Mon Apr 23, 2012 12:49 am

Probability of any one of n equally likely outcomes is 1/n. So if there are 2 choices, 1/2 =.5 or 50% for either choice.

4 choices -- 1/4=.25 or 25% for any one of the 4 choices

3 choices -- 1/3=.333 or 33.3% for any one of the 3 choices.

As far as the upper limit not being included in the random, I think it is kind of normal. For example, if you look at the RAND function in Excel, it generates a number between 0 and 1, and then you can use that to generate a number between a and b using the formula
RAND()*((b-a)+a)
but that won't actually ever generate b. So maybe a formula like that was used to generate Random() in C2.

kittiewan2012-04-23 00:50:25
B
30
S
12
G
13
Posts: 281
Reputation: 12,448

### » Mon Apr 23, 2012 2:00 am

Thank you very much guys.
- Diji
B
24
S
4
G
4
Posts: 54
Reputation: 4,043