-1

I have a program to generate random lottery numbers in WPF but I don't know how to get unique random numbers. I got this exercise after I studied about the 'if structure' so I guess I should use that somehow but I have no idea how.

Here is my code:

private void btnGo_Click(object sender, RoutedEventArgs e)
{

   Random rd = new Random();

    int marginLeft = 50 + (caPaper.Children.Count * 50);

    Ellipse newBall = new Ellipse();
    newBall.Fill = new SolidColorBrush(Colors.Red);
    newBall.Height = 70;
    newBall.Width = 70;
    newBall.Margin = new Thickness(marginLeft, 100, 0, 0);
    caPaper.Children.Add(newBall);

    TextBlock txt1 = new TextBlock();
    txt1.FontSize = 20;
    txt1.Foreground = Brushes.White;
    txt1.Text = " " + rd.Next(1, 45);
    Canvas.SetTop(txt1, 120);
    Canvas.SetLeft(txt1, marginLeft + 20);
    caPaper.Children.Add(txt1);
}
private void btnClear_Click(object sender, RoutedEventArgs e)
{
    caPaper.Children.Clear();
}
Steve
  • 213,761
  • 22
  • 232
  • 286
amur567
  • 67
  • 6

1 Answers1

1

The easiest way to generate lottery style random numers is

  • Create a list of all the possible values
  • Shuffle the list using a robust algorithm (e.g. Fisher Yates)
  • Take the required number of items from the front of the list
Peregrine
  • 4,287
  • 3
  • 17
  • 34