locked
random number RRS feed

  • Question

  • string number=012345679;

    string rr=empty.string;

    int len=10;

    for(int i=0;i<len;i++)

    {

    x=r.next(0,numer.length);

    rr+=x;

    }

    lbl.text=rr;

    this is my code and here what i am trying to do is 

    genarating a random number o length=10 always and storing in database checking is that number exist in database or not if exist left shitinga number and pading a new number example 1 i wANT IT DYNAMICALLY I GETTING BY SINGLE ALUE PADING TO LEFT????

    Monday, May 16, 2016 3:23 PM

Answers

  • Hi radhapatil,

    >> genarating a random number o length=10 always and storing in database checking is that number exist in database or not if exist left shitinga number and pading a new number example 1 i wANT IT DYNAMICALLY I GETTING BY SINGLE ALUE PADING TO LEFT????

    First of all, could you provide a clear demo to explain what you want to achieve and what issue you encountered?

    If you want to create random number, you’d have a look at below code to modify yours.

                string number = "012345679";
    
                string rr = string.Empty;
    
                int len = 10;
    
                Random r = new Random();
                for (int i = 0; i < len; i++)
                {
                    int x = r.Next(0, number.Length);
                    rr += x;
                }
                lbl.text=rr;

    About fetching data from the database, please have a look at below demo that comes from MSDN.

    https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand(v=vs.110).aspx#Anchor_8

    Best Regards,

    Albert Zhang


    • Edited by Albert_Zhang Tuesday, May 17, 2016 2:41 AM
    • Proposed as answer by Albert_Zhang Saturday, May 21, 2016 3:36 PM
    • Marked as answer by DotNet Wang Monday, May 30, 2016 10:41 AM
    Tuesday, May 17, 2016 2:41 AM

All replies

  • Since you are displaying code in C# then perhaps post your question in the Visual C# forum.

    La vida loca

    Monday, May 16, 2016 3:31 PM
  • Hi radhapatil,

    >> genarating a random number o length=10 always and storing in database checking is that number exist in database or not if exist left shitinga number and pading a new number example 1 i wANT IT DYNAMICALLY I GETTING BY SINGLE ALUE PADING TO LEFT????

    First of all, could you provide a clear demo to explain what you want to achieve and what issue you encountered?

    If you want to create random number, you’d have a look at below code to modify yours.

                string number = "012345679";
    
                string rr = string.Empty;
    
                int len = 10;
    
                Random r = new Random();
                for (int i = 0; i < len; i++)
                {
                    int x = r.Next(0, number.Length);
                    rr += x;
                }
                lbl.text=rr;

    About fetching data from the database, please have a look at below demo that comes from MSDN.

    https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand(v=vs.110).aspx#Anchor_8

    Best Regards,

    Albert Zhang


    • Edited by Albert_Zhang Tuesday, May 17, 2016 2:41 AM
    • Proposed as answer by Albert_Zhang Saturday, May 21, 2016 3:36 PM
    • Marked as answer by DotNet Wang Monday, May 30, 2016 10:41 AM
    Tuesday, May 17, 2016 2:41 AM
  • it should not repeat every time i need to add that number in data base , millions of random numbers i want to genarete

    Radhapatil

    Wednesday, June 15, 2016 12:23 PM
  • This really sounds like you haven't thought it through well enough.  Here's what I get:

    • You want to generate a random number.  NO PROBLEM.  You've already discovered Random.Next
    • You want the length (number of decimal digits) to always be 10.  OK...so we're probably talking about a string, not a number (consider leading zeroes).  NO PROBLEM.  You've discovered generating the digits one at a time and concatenating them to form a string.  Good job.
    • You want to store that string in a database.  NO PROBLEM.  The code you use depends on your database, but all databases support storing data.
    • You want to see if you've generated and saved that same random string before.  NO PROBLEM.  Just find the string in your database.  Databases are good at finding information.
    • If the value exists, you want to create a new value by "left shitinga" (which I assume means left-shifting) and "pading" (I assume you mean padding or appending) a new number (I assume you mean digit).  Here's where the trouble begins. The process of shifting the digits left and appending a new digit is not guaranteed to generate a number that the database does not contain.  Also, appending another digit is going to make the length 11, not 10, which may violate your original requirement.
    • You've surprisingly restated your previous goal about (and I paraphrase) padding a single value to the left.  In the previous statement it seems like you wanted to pad to the right.  Here's where I start to get confused.  Hopefully you just made an honest mistake in writing your post.

    My comments and generate computer science advice:

    • Databases support auto-number primary keys.  Use these if local uniqueness is your goal.
    • GUIDs are globally unique and have that feeling of being "random" that you may be desiring.
    • If your randomness requirement comes from wanting to be unable to generate a valid string, then you need to make use of cryptographic techniques to create valid codes.  (Kind of like credit card numbers or software license key strings -- where it's hard to generate a valid string randomly.)
    • Iterating and trying again in the event of a collision is a good idea, but padding a character on each collision will create a bunch of strings like 431, 4311, 43111, 431111, 43111111, 431111111, etc.  It's perhaps better to just increment the existing number until you find a non-collision.  So for example, if 431 exists, then instead of trying 4311 next, you would try 432.  This is what Windows does when it copies files.  If your original is Foo.txt, the copy is Foo - Copy.txt, then Foo - Copy (2).txt then Foo - Copy (3).txt etc.  It knows how to parse out the number part of the appended text.  Otherwise it would be Foo - Copy.txt, then Foo - Copy - Copy.txt, then eventually  Foo - Copy - Copy - Copy - Copy...  (Not an efficient strategy for resolving collisions)
    • You can create a data structure that can efficiently store which values are used and which are available that can be queried in less than O(N) time.  A skip list is reasonable.  Heaps also work.  There are a variety of choices for "set" implementations.  Some just check for existing values, some can also generate missing values.  You seem to want an operation to return an unused random string.  That's easiest and most efficient if you choose a string that is lexicographically adjacent to a used string (the "next" string), then you can use a data structure that store ranges of used values.  A strategy to avoid fragmentation will also be required.  The simplest of these is the "auto number" scheme used by databases, so we're back to my first comment, which is really efficient.
    • And finally, the idea of storing millions of random numbers seems odd (when stated like that.)  If this is some kind of a statistical experiment, then we have the field of statistics in mathematics to help us make calculations and predictions on what happens with large sets of uniformly distributed "random" numbers.  Perhaps there is an opportunity for you to just compute your results directly using statistics rather than keep track of millions of random numbers.  Randomness is really just statistical randomness -- usually meaning uniform distribution.  And that's so rarely what you want in a database.  So again, you should probably just stick to the auto-numbered primary key.
    • Does the string need to be human readable?  human editable?  Visible to the user?  I could fire off a huge number of questions to more clearly determine your requirements.  You should be clear on your requirements and disclose them when asking your questions here in this forum to tell us what your goal is.


    Wednesday, June 15, 2016 2:11 PM