1

This is the second version of this code I've gone through as the first version ran too quickly for the Random function to work properly. I'm trying to make sure none of the randomly generated numbers are the same and if they are replace them in the array but it's not letting me call the array. Can someone tell me what I'm doing wrong?

using System;
using System.Timers;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp4
{
    class Program
    {
        public static Timer aTimer;

        static void Main(string[] args)
        {
            //Create three different random integers and store them
            Random rnd = new Random();
            int RandA = rnd.Next(0, 26);
            int RandB = rnd.Next(0, 26);
            int RandC = rnd.Next(0, 26);

            //Turn those three variables into an array
            int[] RandArray = { RandA, RandB, RandC };

            //Make sure there are no duplicates
            if (RandArray[0] == RandArray[1])
            {
                int RandArray[0] = rnd.Next(0, 26);
            }
            if (RandArray[1] == RandArray[2])
            {
                int RandArray[1] = rnd.Next(0, 26);
            }
            if (RandArray[2] == RandArray[0])
            {
                int RandArray[2] = rnd.Next(0, 26);
            }

            //Print out all three values seperately
            for (int i = 0; i < 3; i++)
            {
                Console.WriteLine("Value of: {1}", i, RandArray[i]);
                continue;
            }
            Console.ReadKey();
        }
    }
}

2 Answers 2

1

Well, I would potentially alter your approach.

public static class Randomizer
{
     private static const generator = new Random();

     public static int Generate(int minimum, int maximum) => generator.Next(minimum, maximum);
}

Then I would do the following:

public static class BuildRandom
{
     public IEnumerable<int> FillCollection(int capacity)
     {
          for(int index = 0; index < capacity; index++)
               yield return Randomizer.Generate(0, 26);
     }

     public static int GetRandomNumber() => Randomizer.Generate(0, 26);
}

Then I would simply do the following.

// Build Collection
var randomized = BuildRandom.FillCollection(5);

// Remove Duplicates
var filter = randomized.Distinct();

while(filter.Count != randomized.Count)
{
     var value = BuildRandom.GetRandomNumber();
     if(!filter.Any(number => number == value))
          filter.Concat(new { value });
}
Sign up to request clarification or add additional context in comments.

Comments

0

int RandArray[0] = is a syntax error. Try RandArray[0] =

1 Comment

Yea that was it. Mind explaining how that was a syntax error though?

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.