# Best way to determine chance?

**URL:** <https://forums.unrealengine.com/t/best-way-to-determine-chance/312988>\
**Category:** Blueprint\
**Tags:** unreal-engine, Blueprint, question, best-practice\
**Created:** [March 28, 2015, 11:35pm UTC](https://forums.unrealengine.com/t/best-way-to-determine-chance/312988 "2015-03-28T23:35:45Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![VigeoJames](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@VigeoJames](https://forums.unrealengine.com/u/VigeoJames)\
**Post date:** [March 28, 2015, 11:35pm UTC](https://forums.unrealengine.com/t/best-way-to-determine-chance/312988/1 "2015-03-28T23:35:45Z")

</div>

I have been using a method of determining chance that basically has an integer that is random and then if that number is higher than the target integer, do something.  
I want to know if there is a better way of determining chance by percentage. I see the integer percentage node but I am unsure how it works.  
So if I wanted something to happen 10% of the time, right now I would do random integer between 1-10 and if the number is equal to 1 let the event happen. Is there another way to achieve this?

---

<div class="post-metadata">

**Author:** ![Azarus](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Azarus](https://forums.unrealengine.com/u/Azarus)\
**Post date:** [March 28, 2015, 11:53pm UTC](https://forums.unrealengine.com/t/best-way-to-determine-chance/312988/2 "2015-03-28T23:53:41Z")

</div>

To determine if something should happen based on percentage:

**IN C++ :**

```
bool ShouldHappen(int percentage)
{
	return (FMath::RandRange(1, 100/percentage)==1?true:false);
}

```

In Blueprints:

> **[Featured Posts - Amirite](https://www.amirite.com)**
>
> Find out who agrees with you. Post your thoughts and instantly be told if you are right!

> **[Featured Posts - Amirite](https://www.amirite.com)**
>
> Find out who agrees with you. Post your thoughts and instantly be told if you are right!

> **[Featured Posts - Amirite](https://www.amirite.com)**
>
> Find out who agrees with you. Post your thoughts and instantly be told if you are right!

- If the input value is 100 then there is 100% chance that it should happen.
- If the input value is 50 then there is 50% chance that it should happen
- and so on…

---

<div class="post-metadata">

**Author:** ![VigeoJames](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@VigeoJames](https://forums.unrealengine.com/u/VigeoJames)\
**Post date:** [March 29, 2015, 12:02am UTC](https://forums.unrealengine.com/t/best-way-to-determine-chance/312988/3 "2015-03-29T00:02:02Z")

</div>

Nice, thank you. I should be able to implement something like this.

---

<div class="post-metadata">

**Author:** ![Azarus](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Azarus](https://forums.unrealengine.com/u/Azarus)\
**Post date:** [March 29, 2015, 12:03am UTC](https://forums.unrealengine.com/t/best-way-to-determine-chance/312988/4 "2015-03-29T00:03:20Z")

</div>

You can create a blueprint function library and create a function so its simpler to reuse 🙂

---

<div class="post-metadata">

**Author:** ![VigeoJames](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@VigeoJames](https://forums.unrealengine.com/u/VigeoJames)\
**Post date:** [March 29, 2015, 12:14am UTC](https://forums.unrealengine.com/t/best-way-to-determine-chance/312988/5 "2015-03-29T00:14:59Z")

</div>

I see it has to do with C+ to actually make a blueprint function library. I know I could do it but it is unknown territory right now. If you know of any tutorials on how to do this that would be great. 😃

---

<div class="post-metadata">

**Author:** ![Azarus](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Azarus](https://forums.unrealengine.com/u/Azarus)\
**Post date:** [March 29, 2015, 12:28am UTC](https://forums.unrealengine.com/t/best-way-to-determine-chance/312988/6 "2015-03-29T00:28:18Z")

</div>

No you don’t need c++ to do function libraries!  
I updated my answer!

---

<div class="post-metadata">

**Author:** ![VigeoJames](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@VigeoJames](https://forums.unrealengine.com/u/VigeoJames)\
**Post date:** [March 29, 2015, 1:30am UTC](https://forums.unrealengine.com/t/best-way-to-determine-chance/312988/7 "2015-03-29T01:30:34Z")

</div>

Awesome! Thanks!

---

<div class="post-metadata">

**Author:** ![KEIBOPLAYS](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@KEIBOPLAYS](https://forums.unrealengine.com/u/KEIBOPLAYS)\
**Post date:** [November 18, 2015, 11:02pm UTC](https://forums.unrealengine.com/t/best-way-to-determine-chance/312988/8 "2015-11-18T23:02:05Z")

</div>

@Azarus im curious on how the

bool ShouldHappen(int percentage);  
{  
return (FMath::RandRange(1, 100 / percentage) == 1 ? true : false);  
}

would work because wouldn’t there be errors in the code? so lets say in the .h file you have bool ShouldHappen;

and in the cpp you structure it that way

bool ShouldHappen(int percentage);  
{  
return (FMath::RandRange(1, 100 / percentage) == 1 ? true : false);  
}

Im asking because im getting errors and having trouble getting this to work, basically i want a 33 percent chance of a health pickup spawning

---

<div class="post-metadata">

**Author:** ![KEIBOPLAYS](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@KEIBOPLAYS](https://forums.unrealengine.com/u/KEIBOPLAYS)\
**Post date:** [November 19, 2015, 10:24pm UTC](https://forums.unrealengine.com/t/best-way-to-determine-chance/312988/9 "2015-11-19T22:24:44Z")

</div>

errors as in trying to return a value in void function

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_b1ca6ba4](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_b1ca6ba4](https://forums.unrealengine.com/u/anonymous_user_b1ca6ba4)\
**Post date:** [August 10, 2016, 4:56pm UTC](https://forums.unrealengine.com/t/best-way-to-determine-chance/312988/10 "2016-08-10T16:56:56Z")

</div>

Is there a better way to determine this because this is broken… anything over a 50% chance becomes 100%

---

<div class="post-metadata">

**Author:** ![Pirog](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Pirog](https://forums.unrealengine.com/u/Pirog)\
**Post date:** [September 1, 2016, 1:34pm UTC](https://forums.unrealengine.com/t/best-way-to-determine-chance/312988/11 "2016-09-01T13:34:16Z")

</div>

there also an option to do this with ‘Random Bool with Weight’:

![105228-chance_bool.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/2/c/32c892193d0af76b1012ed1472169d3b3e0eae49.png)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_c1d802b2](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_c1d802b2](https://forums.unrealengine.com/u/anonymous_user_c1d802b2)\
**Post date:** [March 13, 2018, 11:45am UTC](https://forums.unrealengine.com/t/best-way-to-determine-chance/312988/12 "2018-03-13T11:45:30Z")

</div>

bool ShouldHappen(int Percentage )  
{  
return (FMath::RandRange(1, 100) \<= Percentage ? true : false);  
}

This should yield all values from 1 to 100% chance

---

<div class="post-metadata">

**Author:** ![TextusGames](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TextusGames](https://forums.unrealengine.com/u/TextusGames)\
**Post date:** [July 6, 2018, 7:18pm UTC](https://forums.unrealengine.com/t/best-way-to-determine-chance/312988/13 "2018-07-06T19:18:23Z")

</div>

thank you it works well enough for me.

---

<div class="post-metadata">

**Author:** ![ThatsShocking](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ThatsShocking](https://forums.unrealengine.com/u/ThatsShocking)\
**Post date:** [October 25, 2018, 8:30pm UTC](https://forums.unrealengine.com/t/best-way-to-determine-chance/312988/14 "2018-10-25T20:30:58Z")

</div>

![257652-capture.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/0/1/701868a30c9d1e7f9b665699fef81441e8b882ff.png)

---

<div class="post-metadata">

**Author:** ![real.dude](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@real.dude](https://forums.unrealengine.com/u/real.dude)\
**Post date:** [August 27, 2019, 10:40am UTC](https://forums.unrealengine.com/t/best-way-to-determine-chance/312988/15 "2019-08-27T10:40:05Z")

</div>

You can also use RandomBoolWithWeight

```
 bool UVitalityComponent::ShouldSpawn(float Chance)
 {
      return (UKismetMathLibrary::RandomBoolWithWeight(Chance) == 1 ? true : false);
 }
```

---

<div class="post-metadata">

**Author:** ![KaYseeur](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@KaYseeur](https://forums.unrealengine.com/u/KaYseeur)\
**Post date:** [May 8, 2022, 11:27pm UTC](https://forums.unrealengine.com/t/best-way-to-determine-chance/312988/16 "2022-05-08T23:27:34Z")

</div>

> bool ShouldHappen(int Percentage )  
> {  
> return (FMath::RandRange(1, 100) \<= Percentage ? true : false);  
> }

Here the ternaire is useless

> bool ShouldHappen(int Percentage )  
> {  
> return FMath::RandRange(1, 100) \<= Percentage ;  
> }

> bool UVitalityComponent::ShouldSpawn(float Chance)  
> {  
> return (UKismetMathLibrary::RandomBoolWithWeight(Chance) == 1 ? true : false);  
> }

Same here

> bool UVitalityComponent::ShouldSpawn(float Chance)  
> {  
> return UKismetMathLibrary::RandomBoolWithWeight(Chance);  
> }

already return a bool so just use UKismetMathLibrary::RandomBoolWithWeight(Chance)

---

<div class="post-metadata">

**Author:** ![Nizami\_Nz](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Nizami\_Nz](https://forums.unrealengine.com/u/Nizami_Nz)\
**Post date:** [July 9, 2023, 12:16pm UTC](https://forums.unrealengine.com/t/best-way-to-determine-chance/312988/17 "2023-07-09T12:16:52Z")

</div>

![Random procent %](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/5/b/05b90bb582629517a858c8c10267d1fb9d14edaf.png)  
It’s randim % from (0) to (100).
