-2

I'm trying to code in Unity and I don't know how to make this. My idea is to declare int variables, generating the int randomly inside a range. Later, create a list with the variables and pick one randomly. Finally, display the resulting int in a label. This is my code, which is incomplete and maybe wrong:

#Variables
 int low = Random.Range (100, 120);
 int standard = Random.Range (120, 140);
 int high = Random.Range (140, 160);

 #List
 string List = new string { low, standard, high };

 #Pick one random item from the list
 ???

 #Display that item in a label as an int
 ???

I'm not sure if this is the most effective way to do it. Also, could be possible to display in a label two int with a "/" between? Thanks!

10
  • Why would you make a list of string from int variables ? In your case I would suggest making a list of int instead. Commented Nov 9, 2017 at 23:08
  • I don't think the duplicate will help you that much. By looking at your attempt to create List, you really do need to understand basic C# stuff. You will save yourself so much time by learning them.Here is a basic C# tutorial that includes List. You can also learn C# within Unity. Commented Nov 9, 2017 at 23:09
  • 1
    So your idea is to create three random numbers, one between 100 and 120, one between 120 and 140, and one between 140 and 160, and then choose one of those random numbers at random? How is that in any way different from simply choosing a single random number between 100 and 160? Commented Nov 9, 2017 at 23:29
  • Please only ask one question per question. Commented Nov 9, 2017 at 23:32
  • 1
    Wait, you are saying that there is a statistical difference between "uniformly choose from one of 60 items", and "uniformly choose from one of three bins each containing 20 items, and then uniformly choose an item from the bin"? What statistical test can determine which process was used? In the first case each item has a 1/60 chance; in the second, each item has a 1/3 * 1/20 chance, and I think you'll find that 1/3 * 1/20 is 1/60. Commented Nov 10, 2017 at 1:12

2 Answers 2

0

First of all you are making string array which is wrong, use int type list :

public List<int> RandomNumbers = new List<int>(); 

Then add each random number to list

RandomNumbers.Add(low);
RandomNumbers.Add(standard);
RandomNumbers.Add(high);

//Then select random number from list as int: 

Int pickedNumber = RandomNumbers[Random.Range(0, (myRandomNumber.Count - 1))];

And finally display your number

GUILayout.Label(pickedNumber.ToString()); 
Sign up to request clarification or add additional context in comments.

Comments

-1

Don't know what you mean, but this is a full example of how to generate random numbers and pick them randomly.

public class ExampleClass : MonoBehaviour
{
    public int randomCount = 3;
    public List<int> myRandomNumber = new List<int>();

    [Space(20)]
    public int minRandom = 0;
    public int maxRandom = 255;

    private int pickedNumber; 

    void Start ()
    {
        for (int i = 0; i < randomCount; i++)
        {
            myRandomNumber.Add(Random.Range(minRandom, maxRandom));
        }

        //pick one number randomly
        pickedNumber = myRandomNumber[Random.Range(0, myRandomNumber.Count)];
    }

    void OnGUI ()
    {
        //Display your picked number in game screen
        GUILayout.Label(pickedNumber.ToString());
    }
}

But what's the point of generating a list of random number? while you can instead just generate the number that you want to display with Random.Range() without making a list first.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.