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

  • Home
  • SEARCH
  • 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 3454114
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:27:16+00:00 2026-05-18T09:27:16+00:00

I had a lot of issues with randomizing lists. I am talking about a

  • 0

I had a lot of issues with randomizing lists. I am talking about a list of 200 elements, where I want to shuffle the list. Don’t get me wrong, I read a lot of examples, and on first glance there are pretty nice things, like this:

Randomize a List<T>

But in my experience, at least on a fast machine, this is basically worthless. The shuffling works so fast that there is NOT a MS delay between two calls to Random.NEXT() which results in not nearly random behaviour.

I am NOT talking about super secure stuff, just a basic game random. I know I can add a delay of 1 MS, but this means “wasting” 200 MS just to randomize a list.

Now I found this approach: http://www.codinghorror.com/blog/2007/12/shuffling.html

It looks nice, using GUIDs to sort. BUT aren’t they created the same way? Lets step it up a notch, lets assume I want to create 1000 numbers, between 0 – 5. This code is basically useless:

        var resultA = new List<int>();
        for (int i = 0; i < 1000; i++)
        {
            resultA.Add(new Random().Next(5));
        }


        var resultB = new List<int>();
        for (int i = 0; i < 1000; i++)
        {
            resultB.Add(new Random().Next(5));
            Thread.Sleep(1);
        }

A does not work at all, at least not im my environment in Windows Phone 7. B is fine, but it takes a second, which is also stupid. Any comments or thoughts, it can’t be that hard to create a random list of integers 🙂

  • 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-18T09:27:16+00:00Added an answer on May 18, 2026 at 9:27 am

    Don’t keep initializing a new instance of Random; make just one and continually reference it.

    var random = new Random();
    var resultA = new List<int>();
    for (int i = 0; i < 1000; i++)
    {
        resultA.Add(random.Next(5));
    }
    

    You are correct that repeatedly creating new instances of Random within the same “timestamp” will result in the same seed; but calling .Next on an instance of Random “advances” the seed so that the next number you retrieve is (most likely) different.

    This is also covered in the documentation on Random:

    … because the clock has finite resolution, using the parameterless constructor to create different Random objects in close succession creates random number generators that produce identical sequences of random numbers.

    …

    This problem can be avoided by creating a single Random object rather than multiple ones.

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

Sidebar

Related Questions

I've had a lot of good experiences learning about web development on w3schools.com .
After searching a lot i did not get any answers and finally i had
From discussions I've had about language design, it seems like a lot of people
So I don't do a lot of Win32 calls, but recently I have had
I've been hearing/reading a lot about covariance issues in C# and I wanted to
I know a lot of people have had similar issues to this but I
I've had a lot of trouble trying to come up with the best way
I've had a lot of users complain that the little i info button is
I am a senior level developer but I haven't had a lot of formal
I had a method with a lot of persistence calls that used a nHibernate

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.