Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8403035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:14:13+00:00 2026-06-09T22:14:13+00:00

I’m trying to create a method that produces 10 unique random numbers, but the

  • 0

I’m trying to create a method that produces 10 unique random numbers, but the numbers are not unique, I get several duplicates! How can I improve the code to work as I want?

        int[] randomNumbers = new int[10];

        Random random = new Random();

        int index = 0;

        do
        {
            int randomNum = random.Next(0, 10);
            if (index == 0)
            {
                randomNumbers[0] = randomNum;
                index++;
            }

            else
            {
                for (int i = 0; i < randomNumbers.Length; i++)
                {
                    if (randomNumbers[i] == randomNum)
                        break;
                    else
                    {
                        randomNumbers[index] = randomNum;
                        index++;
                        break;
                    }
                }
            }
        }
        while (index <= 9);

foreach (int num in randomNumbers)
            System.Console.Write(num + " ");

EDIT 2: I have corrected all errors now, but this code isn’t working becuase the numbers are not unique! I have updated my code above to the latest version. I preciate some help to solve this! Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:14:14+00:00Added an answer on June 9, 2026 at 10:14 pm

    random.next(0, 10) returns a random number between 0 and 10. It can happen, that it returns the same number multiple times in a row and it is not guaranteed, that you get every number between 0 and 10 exactly one time, when you call it 10 times.

    What you want is a list of numbers, every number is unique, between 0 and 9 (or 1 and 10). A possible solution to this would be something like this:

    //Create the list of numbers you want
    var list = new List<int>();
    for(var x = 0; x < 10; x++)
    {
        list.Add(x);
    }
    
    var random = new Random();
    //Prepare randomized list
    var randomizedList = new List<int>();
    while(list.Length > 0)
    {
        //Pick random index in the range of the ordered list
        var index = random.Next(0, list.Length);
    
        //Put the number from the random index in the randomized list
        randomizedList.Add(list[index]);
    
        //Remove the number from the original list
        list.RemoveAt(index);
    }
    

    Explained in words, this is what you do:

    1. Create the list with all the numbers, that your final list should contain
    2. Create a second empty list.
    3. Enter a loop. The loop continues, as long as the ordered list still has numbers in it.

      1. Pick a random index between 0 and list.Length
      2. Put this random number in the randomized list
      3. Remove the item at the index position from the ordered list.

    Like this you create the set of numbers you want to have in your randomized list and then always pick a random entry from the ordered list. With this technique, you can also achieve, that certain values occur multiple time in the list.

    I’m not sure if my code compiles against c#, as I currently do not have visual studio running here, but I think the code should be mostly correct.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.