- Home /
Randomly setting a boolean to true?
Say I'd like to randomly set a boolean to true if an object is hit by a raycast. But I'd like to be able to adjust the chance. I was looking over Random.value. Which, returns a random number between 0.0 to 1.0. Could that represent a random value between 0% and 100% chance? How do I go about this?
Answer by Eric5h5 · Aug 24, 2014 at 02:46 AM
var myBool = (Random.value < 0.5);
So, that would be something like
if( myBool < 0.75f){
boolean = true;
}
...?
Answer by SinisterRainbow · Aug 13, 2015 at 07:46 AM
C#:
public void passFail(float fChanceOfSuccess) {
float fRand = Random.Range(0.0f,1.0f);
if (fRand <= fChanceOfSuccess)
return true;
return false;
}
A lot of excess and redundant code there. It's such a trivial thing you don't need a function; see my answer. But if you had to make it a function for some reason, then it would just be:
public void PassFail (float chanceOfSuccess) {
return Random.value < chanceOfSuccess;
}
Eric5h5, while I'm sure you are amazing, when I give answers on posts where a person obviously doesn't have a lot of coding experience, I will suffer elegance for clarity.
That attitude is only valid if you also include the 'and this is the better short-form' in the reply, to give the beginner a chance to learn a better coding-style.
Answer by SatansPineapple · Sep 26, 2021 at 11:13 PM
my solution with int:
private void Start()
{
for (int i = 0; i < 50; i++)
{
int t = UnityEngine.Random.Range(1, 101);
print(t);
print(RandomBool(ref t));
tiles[i].SetActive(RandomBool(ref t));
}
}
//dont do that please dont
private bool RandomBool(ref int value)
{
return value % 2 == 0;
}
//you can also
private bool RandomBool()
{
int value = UnityEngine.Random.Range(1, 101);
return value % 2 == 0;
}
your choice;)
How is that
Randomly setting a boolean to true
just to quote the question title?
There is no "random" in your code at all. Your method name is misleading. All your method does is checking if the value is even or odd. Also why a ref argument? This makes no sense. The value is not changed inside the method and using a ref here just means it will most likely be slower and also limits the usage of the method since you always need an actual variable to pass to your method.
Though the biggest issue is: This question is 6+ years old, the most recent comment is 3 years old...
hi edited a little bit. who cares about the age of question. im just play around and for me it works well and fast enough. so dont be hate
It's bad code though, and your edit isn't much better. Playing around is great, but keep it to yourself while learning, since it doesn't really help anyone.
You could start by explaining why you pass an int as a ref.
If you don't know why you are doing it, then don't do it.
ok wow thanks for your attention. to the ref keyword. maybe i have misunderstood something. i thought when im writing: void Test(ref int value) it doesnt create an int every time i call this method. so yes my fault.
earlier this day here was a comment: it doesnt work. so i edited post and dont look at redundant code or bad code. yes my fault shame on me:)
so let me try a last time:
public bool PassFail(int chanceOfSuccess, int condition)
{
return UnityEngine.Random.Range(1, 101) % chanceOfSuccess == condition;
}
Why use the Random.range (int) in 1-101 (maxExclusive) and not the native Random in float (0.0-1.0)?