Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Random Number genration in c#

Random Number genration in c#

Scheduled Pinned Locked Moved C#
csharpquestionlounge
3 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Suj_78
    wrote on last edited by
    #1

    Hi Guys, The random number in c# using System.Random.NextDouble() is not returning a good set of random numbers over 10000 iterations, there seems to be concentration for a consecutive iterations on set of values, my code looks like this System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test.txt"); for (int i=0;i<10000;i++) { RandomNumber = new Random(); string lines = "iterationCount"+ ":"+" "+i.ToString()+ " "+RandomNumber.NextDouble().ToString(); file.WriteLine(lines); //} } file.Close(); is there a better way of generatin random numbers in c#? Many Thanks

    happy coding!

    C 1 Reply Last reply
    0
    • S Suj_78

      Hi Guys, The random number in c# using System.Random.NextDouble() is not returning a good set of random numbers over 10000 iterations, there seems to be concentration for a consecutive iterations on set of values, my code looks like this System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test.txt"); for (int i=0;i<10000;i++) { RandomNumber = new Random(); string lines = "iterationCount"+ ":"+" "+i.ToString()+ " "+RandomNumber.NextDouble().ToString(); file.WriteLine(lines); //} } file.Close(); is there a better way of generatin random numbers in c#? Many Thanks

      happy coding!

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Suj_78 wrote:

      RandomNumber = new Random();

      Put that line outside the loop. When you create a new Random number generator it is seeded against the current time. If the loop is very short there is no time for the clock to update, so it gets reseeded against the same time as the last iteration, therefore producing the same number again.

      Upcoming FREE developer events: * Developer Day Scotland Recent blog posts: * The Value of Smaller Methods * Creating Many-to-Many joins My website | blog

      S 1 Reply Last reply
      0
      • C Colin Angus Mackay

        Suj_78 wrote:

        RandomNumber = new Random();

        Put that line outside the loop. When you create a new Random number generator it is seeded against the current time. If the loop is very short there is no time for the clock to update, so it gets reseeded against the same time as the last iteration, therefore producing the same number again.

        Upcoming FREE developer events: * Developer Day Scotland Recent blog posts: * The Value of Smaller Methods * Creating Many-to-Many joins My website | blog

        S Offline
        S Offline
        Suj_78
        wrote on last edited by
        #3

        Thanks Colin.

        happy coding!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups