# Best way to determine chance?

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?

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:

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

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

You can create a blueprint function library and create a function so its simpler to reuse 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. No you don’t need c++ to do function libraries!

Awesome! Thanks!

@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

errors as in trying to return a value in void function

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

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

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

This should yield all values from 1 to 100% chance

thank you it works well enough for me.

You can also use RandomBoolWithWeight

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

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)