# How do I make something happen by chance?

• 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

<img src="http://www.freeimagehosting.net/newuploads/k4xaq.png" border="0" />

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.

<img src="http://oi41.tinypic.com/34tbwcl.jpg" border="0" />

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.

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

• Sounds great, thank's Kyatric. Much appreciated.

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

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

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

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

• ## Try Construct 3

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

Construct 3 users don't see these ads
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)