Results 1 to 2 of 2

Thread: Creating a sequence...

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2005
    Posts
    15

    Creating a sequence...

    Hi,

    I am trying to create a sequence so that every time the user clicks the submit button gets a unique 6 digit number... would this be a good start?

    Code:
    public static class CustomSequenceOperators
    {
        public static IEnumerable Combine(this IEnumerable first, IEnumerable second, Func func) {
    .......





    Edit: Fixed [code][/code] tags. - Hack
    Last edited by Hack; Nov 1st, 2005 at 01:14 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Creating a sequence...

    If you want a random 6-digit number then you could use a Random object to generate numbers like this:
    Code:
    Random myRandom = new Random();
    
    string randomNumber = myRandom.Next(0, 1000000).ToString().PadLeft('0', 6)
    Notice that this will give you a string because an actual number can't have leading zeroes. Also note that the Random class produces a psuedo-random sequence, i.e. the same seed produces the same sequence. If you provide no seed then a system timer-based seed is used by default, so the chances of geting the same sequence twice is very slim, unless you create multiple Random objects very close together in code, so that the system timer has not advanced enough to produce a new seed. If you want more random than that then there have been various suggestions recently on the VB.NET forum. A search for "random" should lead you in the right direction. Finally, note that this produces a random number that is likely, but not guaranteed, to be unique. If you want to completely avoid repeated numbers then you would need to somehow keep track of which numbers have been used.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width