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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:00:07+00:00 2026-06-10T15:00:07+00:00

in a C# ConsoleApp, I made a method that will either return true or

  • 0

in a C# ConsoleApp, I made a method that will either return true or false, based on its possibility parameter that you give it, so if you give it 5, there’s a 50% that you’ll get true, same for false, give it 9, there’s a 90% chance that you’ll get true, 10% false.
Here it is:
(I was writing this as a tutorial, and I was explaining it in that
balls-ThatGotNumbers-inside-a-box kinda way to make it clear, hence the name of the variable BallNumber)

static public bool TrueOrFalse(int possiblity)
{
      Random rand = new Random();
      int BallNumber = rand.Next(0, 10); // BallNumber values are from the interval: [0, 10[  (0, 1, 2, ....9)
      if (possiblity <= 0)
        return false;
      if (possiblity == 1)
        return (BallNumber == 0); // any number you like.
      if (possiblity == 2)
        return (BallNumber == 0 || BallNumber == 1); // any two numbers, cuz there's a 20% chance that you might get a ball from the box that has either number n OR m.
      if (possiblity == 3)
        return (BallNumber == 0 || BallNumber == 1 || BallNumber == 2); // any three numbers ... etc you got the point.
      if (possiblity == 4)
        return (BallNumber == 0 || BallNumber == 1 || BallNumber == 2 || BallNumber == 3);
      if (possiblity == 5)
        return (BallNumber == 0 || BallNumber == 1 || BallNumber == 2 || BallNumber == 3 || BallNumber == 4);
      if (possiblity == 6)
        return (BallNumber == 0 || BallNumber == 1 || BallNumber == 2 || BallNumber == 3 || BallNumber == 4 || BallNumber == 5);
      if (possiblity == 7)
        return (BallNumber == 0 || BallNumber == 1 || BallNumber == 2 || BallNumber == 3 || BallNumber == 4 || BallNumber == 5 || BallNumber == 6);
      if (possiblity == 8)
        return (BallNumber == 0 || BallNumber == 1 || BallNumber == 2 || BallNumber == 3 || BallNumber == 4 || BallNumber == 5 || BallNumber == 6 || BallNumber == 7);
      if (possiblity == 9)
        return (BallNumber == 0 || BallNumber == 1 || BallNumber == 2 || BallNumber == 3 || BallNumber == 4 || BallNumber == 5 || BallNumber == 6 || BallNumber == 7 || BallNumber == 8);

      return true; // if you give it a number higher than 10, it will return true ..
}

Now the method is working fine, but I wanted to prove it.
So I came up with this idea:

int TruePossiblity = 0, FalsePossiblity = 0;
for (int i = 0; i < 100; i++)
{
  bool _TRUE = TrueOrFalse(5);
  if (_TRUE) TruePossiblity  += 1;
  else FalsePossiblity  += 1;
}
Console.WriteLine(x);
Console.WriteLine(y);

Now, what should be printed is:

50
50

The Problem, is that It either Prints:

100
0

or

0
100

So, I debugged it, and walked through it, step by step, and got amazed that it actually in the end printed:

50
50

I Immediately inserted System.Threading.Thread.Sleep(1);
right after the assignment of the variable _TRUE
I got better results, but not that good, the more I increase the time, the better results I get (closer results to what it should print, which is 50,50).
when I used 200 Millisecs I actually got the required result 50,50 !

Now, There’s only one thread running, which is the main thread, so there’s no race goin on between threads so that things could mess up and give those unwanted results.

What is goin on ?
Could Somebody explain it ?
Can I avoid using the Sleep ?

Thank you for any tips you provide.

  • 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-10T15:00:09+00:00Added an answer on June 10, 2026 at 3:00 pm

    A Random instance is seeded with the current time. Because the loop executes too fast, it uses the same time, hence you get always the same "random" value.

    Instead of creating the Random in the method itself, you should pass it as argument or use a member variable in the class.

    For example:

    static Random rand = new Random();
    
    static public bool TrueOrFalse(int possiblity)
    {
          int BallNumber = rand.Next(0, 10); // BallNumber values are from the interval: [0, 10[  (0, 1, 2, ....9)
          // ...
    
          return true; // if you give it a number higher than 10, it will return true ..
    }
    

    From MSDN remarks:

    Providing an identical seed value to different Random objects causes each instance to produce identical sequences of random numbers.

    If your application requires different random number sequences, invoke
    this constructor repeatedly with different seed values. One way to
    produce a unique seed value is to make it time-dependent. For example,
    derive the seed value from the system clock. However, the system clock
    might not have sufficient resolution to provide different invocations
    of this constructor with a different seed value. This results in
    random number generators that generate identical sequences of
    pseudo-random numbers, as illustrated by the first two Random objects
    in the following example. To prevent this, apply an algorithm to
    differentiate the seed value in each invocation, or call the
    Thread.Sleep method to ensure that you provide each constructor with a
    different seed value.

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

Sidebar

Related Questions

Basically I made console app that performs some task that takes a few minutes.
I have a dll that I have made that works perfectly. I need to
I have designed a system that is made up of 3 separate applications: Web
I've been trying to deploy a c# console app that I made to a
I have made a console app that adds and subtracts fractions, I have added
I have a long running process called ImportProductInformation called by a consoleapp that I'm
In C#, I made a ClassLibrary that has one Namespace and one Class. I
My console app looks like that. #include stdafx.h #include <iostream> #include <cmath> using namespace
Im currently doing a console app that has to send scheduled emails. In one
I've got a console app that loads up a datatable; I'd like to export

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.