0

It's a favorite panel. You can select numbers (with button click) and than I would like to add this number to an array and than get a random number from this array.

public int runs;
public int randomNumber;
public int[] favorites = new int[75];

public void RandomButton()
{
    if (DataController.Instance.group == 3)
    {
            favorites[randomNumber] = UnityEngine.Random.Range(0, favorites.Length);
            Debug.Log(favorites[randomNumber]);
    }
}

public void b0()
{
    for (runs = 0; runs < favorites.Length; runs++)
    {
        favorites[runs] = 0;
    }
}

public void b1()
{
    for (runs = 0; runs < favorites.Length; runs++)
    {
        favorites[runs] = 1;
    }
}

I'm stuck , because I get random number between 0 - 75. I would like to have a random number from the "favorites" array after I click on the buttons.

  • Use a `List` instead of a fixed size array, it's better suited for your needs. – Gusman Jun 28 '20 at 16:39
  • @Gusman in Unity a public array is initialized via the Inspector and is adjustable by the Developer. So unless you need to add or remove elements on runtime via code a `List` or `int[]` behave basically the same way ;) – derHugo Jun 29 '20 at 12:10
  • @derHugo if you read the question you will see the user is adding dynamically values at runtime via user interaction. And having the `Count` property to know how many numbers are in the list is the key to solve his problem. So, `List` is what he needs. – Gusman Jun 29 '20 at 12:12
  • 1
    @Gusman good point! Actually didn't note that the adding of values was also part of the question – derHugo Jun 29 '20 at 12:23
  • @derHugo The problem is the code has nothing to do with the question, it's totally different what he's asking to what he's doing ;) – Gusman Jun 29 '20 at 12:25

2 Answers2

0

What you are doing here

favorites[randomNumber] = UnityEngine.Random.Range(0, favorites.Length);

Is assign a random value between 0 and 74 to an item in your array .. depending on whatever value randomNumber has at that moment ...

What you rather want to do is actually access the value from the array using the random value as index like

randomNumber = favorites [UnityEngine.Random.Range(0, favorites.Length)];
Debug.Log(randomNumber);

However what difference will it make if you are filling your array with always the same numbers using b0 and b1?

After running these methods all elements are either 0 or 1 anyway ...


Anyway in your question you are also asking for how to Add a number.

You shouldn't use an array for this but rather a List<int> like

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

public void AddNumber(int newNumber)
{
    favorites.Add(newNumber);
}

public void RandomButton()
{
    if (DataController.Instance.group == 3)
    {
            randomNumber = favorites[UnityEngine.Random.Range(0, favorites.Count)];
            Debug.Log(randomNumber);
    }
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
  • I think he wants `var number = favorites[rnd.Next(0, favorites.Length)];`... – Gusman Jun 29 '20 at 12:18
  • That's basically what [`Random.Range(0, favorites.Count)`](https://docs.unity3d.com/ScriptReference/Random.Range.html) (or Length for array) does ... – derHugo Jun 29 '20 at 12:21
  • `Next` upper limit is [exclusive](https://learn.microsoft.com/en-us/dotnet/api/system.random.next?view=netcore-3.1#System_Random_Next_System_Int32_System_Int32_), not inclusive. – Gusman Jun 29 '20 at 12:23
  • 1
    Same for [`Random.Range`](https://docs.unity3d.com/ScriptReference/Random.Range.html) when used with `int` arguments: `Return a random integer number between min [inclusive] and max [exclusive] (Read Only).` – derHugo Jun 29 '20 at 12:24
  • Thanks! Your comment is very helpful but I would like to generate a random number from this array. b0 - add number 0 to position 0 and b1 add number 1 to position 1 so favorites[0,1] and etc.. and from this favorites[] generate a random number 0 or 1 or etc –  Jun 29 '20 at 12:26
  • @Xerodus well currently you are setting **all** elements to `0` in `b0` and **all** elements to `1` in `b1` ... what sense would it make to iterate through the array if anyway only setting one specific entry to a specific value? – derHugo Jun 29 '20 at 12:27
  • I'am a beginner :D should I use instead of `public void b0() { for (runs = 0; runs < favorites.Length; runs++) { favorites[runs] = 0; } } ` simple favorites[0] = 0; –  Jun 29 '20 at 12:30
  • @Xerodus well depends what you want to do there but yes this would basically behave equally ... still unsure though what you are trying to achieve here – derHugo Jun 29 '20 at 13:04
-1
if (DataController.Instance.group == 3)
{
        var randomIndex = UnityEngine.Random.Range(0, favorites.Length);
        Console.WriteLine(favorites[randomIndex]); // random item from your array
}

answer

Roman Epifanov
  • 108
  • 1
  • 6