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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:59:30+00:00 2026-06-01T16:59:30+00:00

We have this class here: public static class Helper { private static readonly Random

  • 0

We have this class here:

public static class Helper
{
    private static readonly Random Random = new Random();
    public static int GetRandomId()
    {
        return Random.Next(int.MinValue, 0);
    }
}

At some point calling .GetRandomId of Helper gives us the same integer – Why and how do avoid/fix this?

Note:
The field Random is a singleton-instance, and this behaviour does not occur in a loop (actually it doesn’t even matter how much time there’s between the calls).

To be even more specific:
At some point the random-machine returns only one value, regardless of the amount of calls! It’s not about the likeliness, uniqueness … – I think that I have (with this implementation) broken my random-instance … how come?

  • 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-01T16:59:32+00:00Added an answer on June 1, 2026 at 4:59 pm

    The Next method returns a pseudo-random number between int.MinValue and 0. It does not guarantee the numbers will be unique. What do you think happens if you call Random.Next(0,10) 11 times?

    To prevent duplicates you’ll need to keep track of which Ids have been issued.

    Alternatively, is there any need for the Ids to be in a random order? Could you just use an incrementing int to generate Ids?

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

Sidebar

Related Questions

I have this class: public static class CsvWriter { private static StreamWriter _writer =
I have the following helper class (simplified): public static class Cache { private static
I have this entities: public class Parent: AllDependant { /*Properties goes here*/ } public
I have this class called SiteAsyncDownload.cs Here's the code: public class SiteAsyncDownloader { WebClient
So I have this class public static class MyClass { static MyClass() { ...
Okay so I have this mode: class Posts(db.Model): rand1 = db.FloatProperty() #other models here
Can I optimize the class assignment here at all? I have to do this
I have this class: public MyClass { public void initialize(Collection<String> data) { this.data =
I have this class: public class Source extends Node { protected DistributionSampler delay ;
I have a situation where a helper class with a static DBContext member is

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.