-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!

manugbf
  • 51
  • 2
  • Why would you make a list of string from int variables ? In your case I would suggest making a list of int instead. – SniperLegacy Nov 09 '17 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](http://www.learncs.org/en/Welcome) is a basic C# tutorial that includes List. You can also learn C# [within](https://unity3d.com/learn/tutorials/s/scripting) Unity. – Programmer Nov 09 '17 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**? – Eric Lippert Nov 09 '17 at 23:29
  • Please only ask one question per question. – Eric Lippert Nov 09 '17 at 23:32
  • @EricLippert Hi. Mi idea is to create an app for medical training purposes, about the blood pressure measurement. That figures relate to the low, standard and high measurements. You can find more information here: http://www.bloodpressureuk.org/BloodPressureandyou/Thebasics/Bloodpressurechart – manugbf Nov 09 '17 at 23:42
  • It's not exactly clear what your desired output should be and why you need three different ints. Can you rephrase the question? – Xarbrough Nov 09 '17 at 23:42
  • @Xarbrough The idea is that the app selects randomly between low, standard or hight before displaying the number. After that, you have to decide which category is. I think if I use only one number between 100 and 160 it wouldn't be so real, statistically saying. – manugbf Nov 10 '17 at 00:06
  • @EricLippert I don't know why I can't see the code you wrote. – manugbf Nov 10 '17 at 00:08
  • @Programmer Thanks for the link, definitely I need to learn a lot more. – manugbf Nov 10 '17 at 00:09
  • 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. – Eric Lippert Nov 10 '17 at 01:12

2 Answers2

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()); 
Voodoo
  • 1,550
  • 1
  • 9
  • 19
-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.

endrik exe
  • 606
  • 3
  • 10