1

I am trying to make a program that generates 6 unique numbers from 1 to 49, a bonus number and a 7 digit extra number I want to check if any of the 6 digits match the current lotto number or the bonus number matches. I also want to check if any of the 7 extra numbers match. I am not sure how I would compare the numbers from each array to each other.

Any help would be appreciated

static void PLayLotto649()
{
    int[] currentNumbers = ChangeLotto49Numbers();
    int[] extraNumbers = ChangeLottoExtraNumbers();
    int[] userLottoNumbers = StartingNumbers();
    int[] userExtraNumbers = AutoGenrateExtraNumbers();

    Console.WriteLine($"\nThe current Lotto 6/49 number are: {string.Join(",",currentNumbers)}" );
    Console.WriteLine($"\nThe current Extra number is:{string.Join(",", extraNumbers)}"); 

    Console.WriteLine($"\nYour 6/49 number is: { string.Join(",", userLottoNumbers)}");
    Console.WriteLine($"\nYour lotto Extra number is: { string.Join(",", userExtraNumbes)}");
}

static int[] StartingNumbers()
{
    int temp;
    int[] lotto = new int[7];

    Random rand = new Random();

    for (int i = 0; i < 7; i++)
    {
        temp = rand.Next(1, 50);
        lotto[i] = temp;
    }

    return lotto;
}

static int[] ChangeLottoExtraNumbers()
{
    int temp;
    int[] extra = new int[7];

    Random rand = new Random();

    for (int i = 0; i < 7; i++)
    {
        temp = rand.Next(0, 7);
        extra[i] = temp;
    }
    Console.Write($"the new extra number is: ");
    for (int i = 0; i < 7; i++)
    {
        Console.Write(extra[i] + " ");
    }
    return extra;
}

static int[] AutoGenrateExtraNumbers()
{
    int temp;
    int[] autoExtra = new int[7];

    Random rand = new Random();

    for (int i = 0; i < 7; i++)
    {
        temp = rand.Next(0, 7);
        autoExtra[i] = temp;
    }
    Console.Write($"the new extra number is: ");
    for (int i = 0; i < 7; i++)
    {
        Console.Write(autoExtra[i] + " ");
    }
    return autoExtra;
}

static int[] ChangeLotto49Numbers()
{
    int temp;
    int[] lotto49 = new int[6];

    Random rand = new Random();

    for (int i = 0; i < 6; i++)
    {
        temp = rand.Next(1, 49);
        lotto49[i] = temp;
    }
    Console.Write($"the new lotto Max winning numbers are: ");

    for (int i = 0; i < 6; i++)
    {
        Console.Write(lotto49[i] + " ");
    }

    return lotto49;
}
1
  • FYI, if you need to pick 6 random unique numbers you can use the Fisher-Yates shuffle. Basically create a list of all the possible numbers, then randomly pick a index into the list and grab the value at that index, then remove it from the list and repeat. What you currently have can result in repeats. Commented Nov 21, 2019 at 4:39

1 Answer 1

1

Try something like this:

static bool IsEqualTo(this int[] source, int[] target) {
    if (source == null && target != null || 
        source != null && target == null || 
        source?.Length ?? 0 != target?.Length ?? 0) {
       return false;
    }

    if (source == null && target == null) {
       return true;
    }

    for (var i = 0; i < source.Length; i++) {
     if (source[i] != target[i]) {
       return false;
      }
    }

    return true;
}

Then to use:

     int[] currentNumbers = ChangeLotto49Numbers();
     int[] extraNumbers = ChangeLottoExtraNumbers();
     if (currentNumbers.IsEqualTo(extraNumbers)) { // do something 
     }
Sign up to request clarification or add additional context in comments.

2 Comments

Or just use Enumerable.SequenceEqual.
@juharr that will throw if either is null, but i get your point :)

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.