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
  1. Home
  2. General Programming
  3. C#
  4. Random number in C#

Random number in C#

Scheduled Pinned Locked Moved C#
csharplounge
3 Posts 3 Posters 0 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.
  • G Offline
    G Offline
    goatstudio
    wrote on last edited by
    #1

    IDGenerator.cs --------------

    public class IDGenerator {
       int min, max;
    	   
    	   
       public IDGenerator () {
          this.min = 1000000;
          this.max = 9999999;
       }		
    		
       public string GetID () {
          Random r = new Random(unchecked((int)DateTime.Now.Ticks)); 
    			
          // Produce a number from 00000 through 99999
          String myNumber = r.Next(min,max).ToString("00000");
          r = null;
          return myNumber;
       }   
    }
    
    D N 2 Replies Last reply
    0
    • G goatstudio

      IDGenerator.cs --------------

      public class IDGenerator {
         int min, max;
      	   
      	   
         public IDGenerator () {
            this.min = 1000000;
            this.max = 9999999;
         }		
      		
         public string GetID () {
            Random r = new Random(unchecked((int)DateTime.Now.Ticks)); 
      			
            // Produce a number from 00000 through 99999
            String myNumber = r.Next(min,max).ToString("00000");
            r = null;
            return myNumber;
         }   
      }
      
      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Simple. Your creating a new Random number generator with each invocation of .GetID() and in creating that generator, your seeding the RNG with the current time, in Ticks. What your doing is telling the new random number generator to start with the seed value and generate a REPRODUCABLE string of numbers. What you should be doing in your IDGenerator class is declaring a class level Random, initializing it once, and using it's .Next method on each call to .GetID().

      public class IDGenerator
      {
      int min, max;

      Random r = new Random(unchecked((int)DateTime.Now.Ticks));
      
      public IDGenerator ()
      {
          this.min = 1000000;
          this.max = 9999999;
      }
      
      public string GetID ()
      {
          // Produce a number from min through max.
          // Use the RNG created at the class level.
          String myNumber = r.Next(min,max).ToString("0000000");
          return myNumber;
      }
      

      }

      Also, random numbers are not guaranteed to be unique during the life of the generator, so review your policies on what this function will acceptibly return. It's entirely possible that it could return the same number twice (or more) in a row. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

      1 Reply Last reply
      0
      • G goatstudio

        IDGenerator.cs --------------

        public class IDGenerator {
           int min, max;
        	   
        	   
           public IDGenerator () {
              this.min = 1000000;
              this.max = 9999999;
           }		
        		
           public string GetID () {
              Random r = new Random(unchecked((int)DateTime.Now.Ticks)); 
        			
              // Produce a number from 00000 through 99999
              String myNumber = r.Next(min,max).ToString("00000");
              r = null;
              return myNumber;
           }   
        }
        
        N Offline
        N Offline
        Nick Parker
        wrote on last edited by
        #3

        The following works for me:

        	int min = 1000000, max = 9999999;
        	Random r = new Random();
        	for(int i = 0; i < 5; i++)
        	{
        		Console.WriteLine(r.Next(min, max));
        	}
        

        - Nick Parker
        My Blog | My Articles

        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