guys my for loop is not working

so I want 5 pair of two random number within a certain ratio and then printing the pairs ,but it skips the loops that don’t match the ratio instead of trying to get new numbers that match it. I don’t understand what I’m doing wrong plz help.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test : MonoBehaviour
{

float r = 100;
float g = 40;

void Start(){
    for (int i = 0; i < 5; i++){
        float randX = Random.Range(r, g * i);
        float randY = Random.Range(r * i, g);
        if (randX >= randY){
            if (randY/randX > .4 && randY/randX < .6){  
                Debug.Log("loop" + i + "fini");
                Debug.Log(randY + "," + randX);
            }else{
                randX = Random.Range(r, g);
                randY = Random.Range(r, g);
            }
        }else{
            if (randX/randY > .4 && randX/randY < .6){
                Debug.Log("loop" + i + "fini");
                Debug.Log(randY + "," + randX);
            }else{
                randX = Random.Range(r, g);
                randY = Random.Range(r, g);
            }
        }
    }
}


}

Hello.

I will not write the code for you. Actually thats one of the “funniest things on coding”

You want to get a result, nd want it by using random, so you dont know how many times will take for thes random function to give you the answer.

So, you need an infinite loop, an infinite loop that is broken whan the condition you need becomes true.

You are doing somethign like this inside the for (so you do this 5 times):

Generate number x using random
Generate Number y using random

if (some condition about x and y you dont want)
    {
    generate again x but NOT using random
    generate again y but NOT using random
    }

But, I ask you, maybe what you want to do is something like this:

Generate number x using random
Generate Number y using random

while (some condition about x and y you dont want)
    {
    generate again x but using random
    generate again y but using random
    }

This way, the code will be inside the while, until find a perfect solution.

BUT: it’s “dangerous” because it can take too miuch time (1 second) to find a result if the conditions are difficult to get, or you can even create a infinite while, where the code can’t go out…

So you can have a counter to prevent this infite loop, so for exampe, it only tries to get the numbers 20 times:

Generate number x using random
Generate Number y using random
int count = 0;

while (some condition about x and y you dont want && count < 20)
    {
    count ++;
    generate again x but using random
    generate again y but using random
    }

I’m not sure if this is what you where asking for… :smiley:

Bye!