0

I'm fairly a beginner in C#.

Is it possible for me to declare local variables using local variables?

I want to generate 4 unique random numbers 0-100 without repeats, but now I'm stuck with these variables. I'm trying to declare these local variables using another local variable data, but I can't, because statements run from top to bottom. For example from the code below: I can't declare index1, because exclude1 hasn't been declared yet. If anyone can help me with this problem or suggest a new method, it would be greatly appreciated. Thanks!

        var rand = new System.Random();
        int index1 = rand.Next(0, 101 - exclude1.Count);
        int index2 = rand.Next(0, 101 - exclude2.Count);
        int index3 = rand.Next(0, 101 - exclude3.Count);
        int index4 = rand.Next(0, 101 - exclude4.Count);
        var exclude1 = new HashSet<double>() { index2, index3, index4 };
        var exclude2 = new HashSet<double>() { index1, index3, index4 };
        var exclude3 = new HashSet<double>() { index2, index1, index4 };
        var exclude4 = new HashSet<double>() { index2, index3, index1 };
auomak
  • 1
  • exclude1.Count the variable exclude1 is not declare how to get the count of undeclared object – umasankar Jul 18 '17 at 05:00
  • You have variables cyclicly dependent on each other. How do you think this should work? In which other programming language you see this working? If you know that `excludes` will be having only 3 items in it then you should directly use 3 instead of using Count of excludes. – Chetan Jul 18 '17 at 05:05
  • You've asked an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Your _real_ question is how to generate a sequence of unique random numbers. The plan you came up with is unworkable, but you're still asking how to implement that plan. Don't do that. Instead, use an algorithm that works and is already answered. Given the small number range of 0-100, a shuffled array seems ideal. See marked duplicates for examples and other ideas. – Peter Duniho Jul 18 '17 at 05:10
  • Possible duplicate (https://stackoverflow.com/questions/18484577/how-to-get-a-random-number-from-a-range-excluding-some-values) – MKR Jul 18 '17 at 05:10
  • I wanted to have 4 different variables for the 4 unique numbers generated because I want to have it displayed on 4 separate UI text boxes in Unity. – auomak Jul 18 '17 at 05:19

1 Answers1

1

Is it possible for me to declare local variables using local variables?

Of course you can declare a local variable using other local variables, provided that the local variables that you use have been defined prior to the variable you want to define.

In your case, you have this:

int index1 = rand.Next(0, 101 - exclude1.Count);

where you need exclude1, which is declared after the index1. Even if you move the declaration of exclude1 prior to index1 you would have problems, since as it clear from below:

 var exclude1 = new HashSet<double>() { index2, index3, index4 };

the exclude1 needs index2, index3, index4 and index2 needs exclude2, which contains index1 ! So the code as it is not valid.

If you want to get 4 unique random numbers in the range [0,100], you could try the following:

var numbers = new List<int>();
var random = new Random();
while(numbers.Length < 4)
{
    int number = random.Next(0,101);
    if(!numbers.Contains(number))
    {
        numbers.Add(number);
    }
}
Christos
  • 53,228
  • 8
  • 76
  • 108
  • I'm also working with Unity, I want to display the 4 unique numbers onto 4 separate UI text boxes. – auomak Jul 18 '17 at 05:14