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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:57:36+00:00 2026-05-25T19:57:36+00:00

Possible Duplicate: Random number generator not working the way I had planned (C#) I

  • 0

Possible Duplicate:
Random number generator not working the way I had planned (C#)

I created a method that returns me a random number:

public static int SelectRandomMachine(int max)
{
int seed = (int)DateTime.Now.Ticks;
Random rndNumber = new Random(seed);
int randMachine = rndNumber.Next(0, max);
return randMachine;
}

if I call the method two times, currently it’s return me the same random number:

randM1 = SelectRandomMachine(maxNumber);
randM2 = SelectRandomMachine(maxNumber);

any suggestion would be highly appreciated.

  • 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-05-25T19:57:37+00:00Added an answer on May 25, 2026 at 7:57 pm

    Hint look at this line:

    int seed = (int)DateTime.Now.Ticks;
    

    If you execute that line twice in quick succession, what do you think the values will be?

    For example:

    int seed1 = (int)DateTime.Now.Ticks;
    int seed2 = (int)DateTime.Now.Ticks;
    
    // Write it out *after* executing; console output can take a while
    Console.WriteLine(seed1);
    Console.WriteLine(seed2);
    

    See my article on randomness for solutions and more information.

    EDIT: Here’s a quick and dirty example of the lack of thread safety causing problems:

    using System.Collections.Generic;
    using System.Threading;
    
    class Program
    {
        const int Iterations = 1000000;
        static readonly Random rng = new Random();
    
        static void Main(string[] args)
        {
            List<Thread> threads = new List<Thread>();
            for (int i = 0;  i < 8; i++)
            {
                Thread t = new Thread(ExerciseRandom);
                threads.Add(t);
                t.Start();
            }
            foreach (Thread t in threads)
            {
                t.Join();
            }
            Console.WriteLine(rng.Next());
            Console.WriteLine(rng.Next());
            Console.WriteLine(rng.Next());
        }
    
        static void ExerciseRandom()
        {
            for (int i = 0; i < Iterations; i++)
            {
                rng.Next();
            }
        }
    }
    

    Output on my box:

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

Sidebar

Related Questions

Possible Duplicate: Random number generator not working the way I had planned (C#) I
Possible Duplicate: Random number generator not working the way I had planned (C#) I
Possible Duplicates: Random numbers in C# Random number generator not working the way I
Possible Duplicate: Why does it appear that my random number generator isn't random in
Possible Duplicate: Why does this Random Number Generator not random? I have this test
Possible Duplicate: Why does it appear that my random number generator isn't random in
Possible Duplicate: How to create my own JavaScript Random Number generator that I can
Possible Duplicate: True random number generator I have worked with random functions in python,ruby,
Possible Duplicates: Why does it appear that my random number generator isn't random in
Possible Duplicate: random a 512-bit integer N that is not a multiple of 2,

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.