user3597671
user3597671

Reputation: 3

generating the same random number

    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?

Upvotes: 0

Views: 115

Answers (4)

David Xu
David Xu

Reputation: 5597

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);
}

Upvotes: 0

Fallenreaper
Fallenreaper

Reputation: 10682

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);
}

Upvotes: -2

p.s.w.g
p.s.w.g

Reputation: 148980

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.

Upvotes: 0

Andrew
Andrew

Reputation: 5083

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.

Upvotes: 4

Related Questions