Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Random.Range help

Discussion in 'Scripting' started by darklight98, Feb 4, 2015.

  1. darklight98

    darklight98

    Joined:
    Jan 24, 2013
    Posts:
    18
    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:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. public class TextureGen : MonoBehaviour {
    5.  
    6.     public Material[] ceiling;
    7.     public Material[] wall;
    8.     public Material[] floor;
    9.     public Material[] materials;
    10.  
    11.     void Start () {
    12.         materials[0] = Generate (floor);
    13.         materials[1] = Generate (ceiling);
    14.         materials[2] = Generate (wall);
    15.  
    16.         renderer.materials = materials;
    17.     }
    18.     Material Generate (Material[] used) {
    19.         Material UsedMaterial = used[Random.Range(0, used.Length + 1)];
    20.         return UsedMaterial;
    21.     }
    22. }
     
    Last edited: Feb 9, 2015
  2. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Noticed you have an extra semicolon just before your last square bracket after your random range.
     
  3. Dinesh-Kannan-S

    Dinesh-Kannan-S

    Joined:
    Feb 12, 2013
    Posts:
    16
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
  5. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
  6. darklight98

    darklight98

    Joined:
    Jan 24, 2013
    Posts:
    18
    I kinda solved this problem on another script, but it took too much for it, random should be easier.
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    This will cause an out of range exception:

    Code (csharp):
    1. Material UsedMaterial = used[Random.Range(0, used.Length+1)];
    --Eric
     
  8. darklight98

    darklight98

    Joined:
    Jan 24, 2013
    Posts:
    18
    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.
     
  9. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    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.
     
  10. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Gotta ask the stupid question, is there more than 2 materials in each of the floor, ceiling & wall arrays?
     
  11. darklight98

    darklight98

    Joined:
    Jan 24, 2013
    Posts:
    18
    floor = 5; ceiling = 3; wall = 6;
     
  12. darklight98

    darklight98

    Joined:
    Jan 24, 2013
    Posts:
    18
    And I fixed the array.length + 1 thing, still giving same textures.
     
  13. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    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 ?
     
  14. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    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
    Code (CSharp):
    1. Debug.Log("There are " + used.Length + " options");
    2. int randomIndex = Random.Range(0,used.Length);
    3. Debug.Log("I have chosen number " + randomIndex);
    4. return used[randomIndex]
     
  15. darklight98

    darklight98

    Joined:
    Jan 24, 2013
    Posts:
    18
    The problem is I don't get values higher than 3 using random.
     
  16. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    A valuable information in that case is
    Code (csharp):
    1. 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.
     
  17. darklight98

    darklight98

    Joined:
    Jan 24, 2013
    Posts:
    18
    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
     
  18. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    I guess you should show your current script, because what you wrote makes not sense to me at all.
     
  19. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Just noticed something too, you can't put using... inside the monobehavior class.

     
  20. ReeVee

    ReeVee

    Joined:
    Feb 3, 2014
    Posts:
    5
    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.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TextureGen : MonoBehaviour {
    5.  
    6.     public Material[] floor;
    7.     public Material[] ceiling;
    8.     public Material[] wall;
    9.     private Material[] materials = new Material[3];
    10.  
    11.     void Start () {
    12.         materials[0] = Generate(floor);
    13.         materials[1] = Generate(ceiling);
    14.         materials[2] = Generate(wall);
    15.         renderer.materials = materials;
    16.     }
    17.  
    18.     Material Generate (Material[] used) {
    19.         int r = Random.Range(0, used.Length);
    20.         print("Random Value: " + r);
    21.         return used[r];
    22.     }
    23. }