Home > Mobile >  Generate random array float Unity
Generate random array float Unity

Time:09-06

Im triying to generate a random array of float in start of lenght 25, then using random.range on the start to give a value btwn 1 to 10.

public int counter = 0;
public float[] floats = new float[25];
// Start is called before the first frame update
void Start() 
{
    while (counter < numbers.Length)
    {
        counter  ;
    }
        foreach (float value in floats)
    {
        Random.Range(1f, 11f);
        print(floats[counter]);
    }
}

Ik im doing smt wrong cuz im triying to use a while there too to count the lenght but... ye... thanks in advance!

CodePudding user response:

You need to assign each element of the array:

for (int i = 0; i < floats.Length; i  )
{
   floats[i] = Random.Range(1f, 11f);
}

CodePudding user response:

while (counter < numbers.Length)
{
   counter  ;
}

Not sure what you're trying to achieve here.

You could just use counter = numbers.Length -1; instead of increasing counter until it reaches numbers.Length -1.

Here:

foreach (float value in floats)
{
   Random.Range(1f, 11f);
   print(floats[counter]);
}

You traverse over your float array. You do not assign the return value of Random.Range(1f, 11f); You then you then use counter to index your floats array but counter does not change within that loop.

for (int i = 0; i < floats.Length; i  )
{
   floats[i] = Random.Range(1f, 11f);
}
  • Related