0
    private int left;
    private int middle;
    private int right;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

        Random r = new Random();
        left = r.Next(1, 4);
        middle = r.Next(1, 4);
        right = r.Next(1, 4);

        while (left == middle)
        {
            left = r.Next(1, 4);
            middle = r.Next(1, 4);                
        }

        while (left == right)
        {
            left = r.Next(1, 4);
            right = r.Next(1, 4);
        }

        while (middle == left)
        {
            middle = r.Next(1, 4);
            left = r.Next(1, 4);
        }

        while (middle == right)
        {
            middle = r.Next(1, 4);
            right = r.Next(1, 4);
        }

        while (right == left)
        {
            right = r.Next(1, 4);
            left = r.Next(1, 4);
        }

        while(right == middle)
        {
            right = r.Next(1, 4);
            middle = r.Next(1, 4);
        }
}

so far this is what i have, but i don't want the three variables to have the same number, i thought the while loops would fix that but it didn't. I'm still new to c# does anyone have any suggestions?

5
  • 2
    you need to randomize the seed for the random object Commented May 2, 2014 at 21:09
  • @Fallenreaper: shouldn't matter. Commented May 2, 2014 at 21:10
  • Works fine for me. Running the code (without the while parts) I got 1,2,2. Do you mean you want all three values to be different? Commented May 2, 2014 at 21:12
  • I think thats what he wants Commented May 2, 2014 at 21:12
  • Those loops don't prevent numerical equality. They only test two variables at a time. You should use a single loop to test all three variables. Commented May 2, 2014 at 21:12

4 Answers 4

4

It might be easier to do something like this:

var r = new Random();
var numbers = new List<int> { 1, 2, 3 }.OrderBy(n => r.Next()).ToList();
left = numbers[0];
middle = numbers[1];
right = numbers[2];

This will shuffle the numbers you want and then you can assign them to your three variables. It is also guaranteed to not loop forever, since you know your values are already unique.

Sign up to request clarification or add additional context in comments.

2 Comments

+1. This is definitely the way to go. This is basically a "random assignment without replacement" problem. There's no need for a while loop when you can just guarantee that all values are different from the beginning.
@user3597671 If this (or any other) answer resolved your issue, you should consider accepting it. This helps other users know that problem has been resolved, and it helps future visitors that may have a similar issue know that this answer might help them too. See What should I do when someone answers my question?
0

So you want the numbers 1, 2, and 3 in some random order? One solution would be to create a list and pull items off in a random order. Here's a simple example:

Random r = new Random();
var numbers = new List<int> { 1, 2, 3 };
left = TakeRandom(numbers, r);
middle = TakeRandom(numbers, r);
right = numbers[0];

...

private static int TakeRandom(List<int> list, Random r)
{
    var index = r.Next(0, list.Count);
    var result = list[index];
    list.RemoveAt(index);
    return result;
}

Of course, there may be more elegant ways to write this (extension methods come to mind) but I think this gets the point across.

2 Comments

I don't know why this answer was downvoted; it's a reasonable approach. I would rename GetRandom to TakeRandom, though, to convey the fact that it removes an item from the list.
@ThomasLevesque I'd agree with that. Updated.
0

Use this loop instead to ensure all 3 are different

while (left == middle || middle == right || right == left) {
    left = r.Next(1,4);
    middle = r.Next(1,4);
    right = r.Next(1,4);
}

2 Comments

It should be ||, not &&
@ThomasLevesque In fairness, it depends on how you read the question. OP's phrasing isn't entirely clear: "but i don't want the three variables to have the same number", although given the attempt to solve the problem I strongly suspect OP wants three different numbers.
-2

Why dont you give this a shot?

Random r = new Random();
left = r.Next(1,4), middle = r.Next(1,4), right = r.Next(1,4);
while ( left == middle || middle == right || left == right){
   left = r.Next(1,4); middle = r.Next(1,4); right = r.Next(1,4);
}

--or--

Random r = new Random();
left = r.Next(1,4); middle = r.Next(1,4); right = r.Next(1,4);
while ( left == middle ){
   middle = r.Next(1,4);
}
while (left == right || middle == right){
   right = r.Next(1,4);
}

2 Comments

It should be ||, not &&, in the first code snippet. But anyway, it's a very inefficient way to do it, especially when there are only 3 possible values...
Also, the syntax is wrong; assignments separated by commas is not legal in C#

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.