okrus
okrus

Reputation: 91

How to generate random numbers? First and last number has to be the same

I need to generate 10 different numbers(integers). My problem is that the first and last number has to be the same. How can I make a code for this logic? The numbers are later used to populate a polar chart.

Random random = new Random();

int randomNumber = random.Next(5, 16);
int firstRand = 0;
firstRand = randomNumber;

if(indataInt2 == 0)
{
    firstRand = randomNumber;
}
else if(indataInt2 >= 360 && firstRand != randomNumber)
{
    randomNumber = firstRand;
}

Upvotes: 1

Views: 419

Answers (2)

user9887357
user9887357

Reputation:

First things first, when using the Random class you can provide a seed in which will specify how the number is generated. Therefore I provided a seed for you. This seed is always changing so the random number will always be different. Remember, Random isn't Random, Random(Seed) is Random! The list in which you are looking for is named 'Numbers'.

Hopefully this code can help you:

using System.Collections.Generic;
using System;

namespace Degubbing
{
    class DebugProgram
    {
        static void Main(string[] args)
        {
            List<int> Numbers = new List<int> { };

            int Seed = DateTime.Now.Millisecond;
            Random Generator = new Random(Seed);

            for (int i = 0; i < 10; i++)
            {
                int RandomNum = Generator.Next(10000000, 20000000);
                string Result = RandomNum.ToString();
                Result = Result.Remove(Result.Length - 1);
                Result = Result + Result[0];
                Console.WriteLine(Result);
            }

            Console.ReadKey();
        }
    }
}

Upvotes: 1

Alex Leo
Alex Leo

Reputation: 2851

Something like this should do the job

        List<int> randomNumber = new List<int>();
        Random random = new Random();

        for (int i = 0; i < 9; i++)
        {
            randomNumber.Add(random.Next());
        }

        randomNumber.Add(randomNumber[0]);

Upvotes: 3

Related Questions