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 7827711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:00:27+00:00 2026-06-02T10:00:27+00:00

I am trying to get a unique random number but i always get the

  • 0

I am trying to get a unique random number but i always get the same number every time i run the code. i will get 14 first then 6 but my list for holding all the used numbers doesn’t seem to work.adding the 14 manually works but when i add randInt it doesn’t work.

const int numCards = 32;
        List<int> usedCards = new List<int>();
        int randInt = 0;
        Random rand = new Random(numCards);
        usedCards.Add(14);
        do
        {

            randInt = rand.Next(0, numCards);
            MessageBox.Show(randInt.ToString(), "End Game", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

        }
        while (usedCards.Contains(randInt));

        usedCards.Add(randInt);
  • 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-02T10:00:29+00:00Added an answer on June 2, 2026 at 10:00 am

    Replace:

    Random rand = new Random(numCards);
    

    with

    Random rand = new Random();
    

    Supplying a fixed seed value (numCards always has the same value) in the Random constructor call will result in a predictable, reproducible sequence which is always the same for the same seed value, just like this (not quite but still the point is valid):

    enter image description here

    For example using a fixed seed of 1 and drawing 10 numbers ranging from 0 to 100, on my machine always produces the sequence

    24,11,46,77,65,43,35,94,10,64
    

    Using no seed value on the other hand, the sequence becomes unpredictable.

    Without a seed value passed in, Random will generate a new seed value based on the current time, that’s what you want to get a new sequence of random numbers – provided you don’t initialize it again in fast order, that’s why you should re-use Random instances instead of re-creating them every time.

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

Sidebar

Related Questions

I am trying with below code to generate 10 digits unique random number. As
I'm trying to get android unique id i'm using this code to get android
I'm trying to get random numbers between 0 and 100. But I want them
I have a problem trying to get some code that returns unique answers to
I'm trying to get unique IDs for object instances in PHP 5+. The function,
I've been trying to get the UNIQUE constraint placed on some attributes I have
I'm trying to get the top N records for each unique row of data
Trying to get comfortable with jQuery and I have encountered some sample code that
I have 2 arrays that I'm trying to get the unique values only from
I am trying to get unique values from a table in oracle via sql.

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.