Array question

Hi all
When I run this code the array has 0’s as well as the 1 2 and 3 I assign instead of just the assigned values 1 2 and 3 why?

 void Start()
    { rando =new int [49] ;
        { for (int r = 0; r < 49;r=r+1){
           
           if(Random.value > 0.5){
rando[r]=1;
           }
           if(Random.value > 0.8){
rando[r]=2; 
           }
        
     
        if(Random.value > 0.3){
rando[r]=3;
     
       }


        {  
           
       Debug.Log(rando[r]);

Two reasons:

  • Zero is the default integer (so that’s what is IN the array)

  • Not all of your if statements might fire.

Even if you meant to make one of those less-than, remember every call to Random.value is FRESH number from 0.0 to 1.0.

Not sure exactly what you’re doing , but this might be insightful to you:

1 Like

Hi thx for the video
- Not all of your if statements might fire.
yikes I don’t like the sound of that what would cause an if statement not to fire that must be what is happening to give the zero?Do you have a link for ways to avoid it happening or suggestions to ensure it does not occur.Off the top of my head I could have a line that states if a value in array=0 do the statement over till it equals one of the 3 options given.

Random.value returns a value between 0 and 1, yet your ifs only check for > 0.3. Any value <= 0.3 will end up with a zero in your array because 0 is the default value and you are not changing that.

1 Like

Well you could imagine your code like this:

You roll 3 dice, one after the other (that’s your 3 Random.value accesses).
Only when your first die roll comes up with a 4,5 or 6, you set the current element to “1”
Now you roll your second die. Only when the second die comes out as a 6, you set / overwrite the current element to / with “2”
Now you roll your third and last die. When you roll a 3,4,5 or 6 is that third die, you set / overwrite the current element to / with “3”.

Just imagine your first die rolls a “2”. That means, the first if statement is not true since only values 4,5 or 6 make the if statement true, so it does nothing. Next, imagine your second die rolls a “5”. The second if statement will also be skipped since only a “6” will make the condition true. So the second if statement also does nothing. Finally imagine your third die rolls a “1” which is not on the positive outcomes for the third if statement. So it also does nothing. As a result, if such a combination comes up, none of the if statements will be true, so none will set the element and the element keeps its original value which is “0”.

Your overall approach is flawed. Rolling 3 random number will completely mess up your probabilities. Especially since they are not in any ascending or descending order. Also you don’t have an else-if chain, so later events can overwrite the ones earlier. You will actually get probabilities like this:

0 - 12%
1 - 12%
2 - 6%
3 - 70%

You usually want to roll a single random number and essentially chop up the total range between 0 and 1 and assign each section to a certain outcome. You want to have 3 different outcomes, so there will be 3 sections so two “splitters”. So for example, if you want to get a “1” 50% of the time and a “2” 20% of the time, you get automatically a 3 30% of the time since the probabilities always have to add up to 100%. What you would usually do is something like this

float rand = Random.value;
if (rand < 0.5f)
    rando[r]=1;
else if (rand < 0.5f + 0.2f) // Acutally rand < 0.7f
    rando[r]=2;
else
    rando[r]=3;
float rand = Random.value;
if (rand < 0.5f)
    rando[r]=1;
else if (rand < 0.5f + 0.2f) // Acutally rand < 0.7f
    rando[r]=2;
else
    rando[r]=3;

So you get those 3 ranges:

0   - 0.5  50% => 1
0.5 - 0.7  20% => 2
0.7 - 1.0  30% => 3

Note that the else if chain will always ever allow a single if statement to actually fire. The last one is just an else since it has to catch “all the rest”. Just like when splitting a bill in a restaurant. Each person pays his part, but the last one has to pay all what’s left over on the bill. Usually, if everyone else has paid for their stuff, the remaining items is what the last one had. In the end the whole bill has to be paid.

1 Like

Yea I turned off my computer right after posting the question and realised that was the cause.I will change the >8 to a less than <3 I guess.

Again, reason through it the way @Bunny83 did above: that will not fix the problem because each Random.value is a new fresh number, and you are asking for three of them per entry in your array.

Let’s pretend you changed the third of the above if statements to be less than 0.8:

first number is 0.1 - does not fire
second number is 0.2 - does not fire
third number is 0.9 - does not fire

You want to roll ONE random number per entry in the array, and ask if it is within those ranges.

Thank you for the detailed explanation lot to unpack there.My first time trying random.value so I see my first attempt was horribly full of logic holes.random.value may be to restrictive for my need might try the approach in the posted video but will tinker a bit more with your code see if I can get it to scale to a much larger number of values needed

Thank you all for the help .

1 Like