# Random.Range help

Probably there is a post with this purpose, but I don’t have time to find it. Sorry.
But anyway, I am trying to generate random numbers to use as textures on my map, but it always end up being with 2 equals results, being not so random. I needed it to be like: 3/7/1 or something like that, but I usually get: 2/1/2, 0/1/0, 0/0/2.

Here is my code:

``````using UnityEngine;
using System.Collections;
public class TextureGen : MonoBehaviour {

public Material[] ceiling;
public Material[] wall;
public Material[] floor;
public Material[] materials;

void Start () {
materials[0] = Generate (floor);
materials[1] = Generate (ceiling);
materials[2] = Generate (wall);

renderer.materials = materials;
}
Material Generate (Material[] used) {
Material UsedMaterial = used[Random.Range(0, used.Length + 1)];
return UsedMaterial;
}
}
``````

Noticed you have an extra semicolon just before your last square bracket after your random range.

If you want the same set of Random numbers to be generated, you can check out the Random.Seed

http://docs.unity3d.com/ScriptReference/Random-seed.html

which will help a lot to generate the procedural generation of levels and textures

Don’t have time to format your code either? Shame I don’t have time to answer this now.

Creating a non repeating random list is pretty easy.

I kinda solved this problem on another script, but it took too much for it, random should be easier.

This will cause an out of range exception:

``````Material UsedMaterial = used[Random.Range(0, used.Length+1)];
``````

–Eric

But reading the documentation I found that on Random.Range it won’t reach the max value, so I thought using + 1 would fix it.

You don’t want it to reach the max value. If you have an array with 8 items in it then their indexes go from 0-7. That’s the main reason the upper bound is exclusive in most random int methods.

Gotta ask the stupid question, is there more than 2 materials in each of the floor, ceiling & wall arrays?

floor = 5; ceiling = 3; wall = 6;

And I fixed the array.length + 1 thing, still giving same textures.

I’m confused.

So the reference arrays hold Material objects
floor = 5; ceiling = 3; wall = 6;

Can we assume the Material objects aren’t repeated ?

You are selecting one Material from each reference array selected by a random index.

What does it matter if each random index happens to be the same … If that happens it’s just the nature of random, surely ??

It seems to me you are getting what you wanted
At each start
a random floor : 1 of 5
a random ceiling : 1 of 3
a random wall : 1 of 6

Did you want something different to this ?

I think the issue is that although hes got 6 walls, it keeps picking 0 1 or 2

Would be useful for OP to debug.Log some things in the Generate function, like

``````Debug.Log("There are " + used.Length + " options");
int randomIndex = Random.Range(0,used.Length);
Debug.Log("I have chosen number " + randomIndex);
return used[randomIndex]
``````

The problem is I don’t get values higher than 3 using random.

A valuable information in that case is

``````Debug.Log ("Length: " + used.Length)
``````

If you don’t get values that are greater than 3, it is most likely that the length is not greater than that.

I used yield to fix that, when I put a 0,5s yield, the value go to 3 to 5. Thats what I think its weird

I guess you should show your current script, because what you wrote makes not sense to me at all.

Just noticed something too, you can’t put using… inside the monobehavior class.

Give this a try I have tested it and it works fine. Main change was to size the materials array as it was throwing an index out of range error when trying to set values. Also changed order of array creation so that they match the call order so that the values output to the console for testing match the order in the script component.

Note: That Random.Range for an int will return when passed 0,5 a value of 0,1,2,3 or 4 the min value being inclusive and the max value being exclusive, so if your array has 5 elements with an index range from 0 to 4 the used.Length is the proper max value to pass.

``````using UnityEngine;
using System.Collections;

public class TextureGen : MonoBehaviour {

public Material[] floor;
public Material[] ceiling;
public Material[] wall;
private Material[] materials = new Material[3];

void Start () {
materials[0] = Generate(floor);
materials[1] = Generate(ceiling);
materials[2] = Generate(wall);
renderer.materials = materials;
}

Material Generate (Material[] used) {
int r = Random.Range(0, used.Length);
print("Random Value: " + r);
return used[r];
}
}
``````