This site uses strictly necessary cookies. More Information

X- Home /

# probability for a Void

Hello is there a system for an probability or chance to call an specific void when you are playing?

so like there is a 10% chance for the first function and a 60% chance for the second function.

{

```
void GetMoney()
{
}
void WinAprice()
{
}
```

}

**Answer** by Earthshine
·
May 27 at 09:11 PM

There is a system to get a random number that you may use wherever you want.

```
public void TestYourLuck()
{
float f = Random.Range(0f, 1f); //this sets a new variable to a random number between 0 and 1
if (f <= 0.1f) GetMoney(); //10% chance to Get Money
else if (f >= 0.4f) WinAPrize(); //60% chance to Win a Prize
}
```

In this case GetMoney() and WinAPrize() are mutually exclusive.

Note that `Random.Range(0, 1); `

will always return `0`

. There are two overloads of the Range function, one that takes integer parameters and returns an integer and one that takes float parameters and returns a float value. Furthermore the integer version has the upper limit exclusive while the float version hat the max value inclusive. So for example `Random.Range(0, 3)`

can only return the values 0, 1 or 2 and never 3.

So you need to use float arguments to get a float result `Random.Range(0f, 1f)`

. However if you just want a random float value between 0 and 1, you can simply read the value property

```
float f = Random.value;
```

This is equivalent to

```
float f = Random.Range(0f, 1f);
```

This is correct.

I have forgotten about the overloads.

I'll fix my answer.

Thanks.

Another minor detail:

This comparison:

```
if (f <= 0.1)
```

will promote / cast your float value `f`

to a double since your `0.1`

is a double as well. Since most floating point types in Unity are floats and not doubles you should get the habit of using float literal values like this:

```
if (f <= 0.1f)
```

This will actually do a float comparison since both operands are float values.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Clean way for percentages 0 Answers

Random drop rate problem 2 Answers

Random.range 1 Answer

Spawn chance in my spawner. 2 Answers

2 items with same loot table weight 1 Answer