7 digit unique random number

I would like to generate 7 digit unique random number. I have it something like this :
Random generator = new Random();
          generator.setSeed(System.currentTimeMillis());
          //generates seven digit random number.
          return generator.nextInt(9999999) + 999999;
But it generates 8 digit numbers sometimes. I am giving the Max limit in the braces and min limit after the "+" sign. Could anybody help me figure out what is wrong.

This is not a horrible solution but an intelligent &
easy solution ;)Actually this would be the easy and intellegent solution to the problem "give a random 7 digit integer."
Random generator = new Random();
//generates seven digit random number.
return generator.nextInt(10000000);Though the orginal post does not specify this as part of the problem others have assumed (probably correctly) that in fact the poster wants a number between 1000000 and 9999999 (inclusive) which can easily be produced with the following modification to the above.
Random generator = new Random();
//generates seven digit random number.
return generator.nextInt(9000000) + 1000000;I am confused by your response. Are you saying the becasue the original question was asking why flawed code doesn't work, that makes your solution good?

Similar Messages

  • How to generate a unique random number in a MySQL db

    I'm creating a volunteer and also a separate vendor application form for an airshow. The volunteer and vendor info is stored in separate tables in a MySQL db, one row per volunteer or vendor. There will be about 100 volunteers and 50 vendors. When the application is submitted it should immediately be printed by the applicant, then signed and mailed in. This past year we had problems with some people who didn't immediately print their application so I'd like to still give them the option to immediately print but also send them an e-mail with a link to their specific row in the MySQL db. I have an autoincrement field as the primary key for each table, but I think sending this key to the applicant in an e-mail would be too easy for them to guess another id and access other people's info.
    I'm thinking I should add a column to each table which would contain a unique random number and I would then send this key in the e-mail to the applicant. So, can anyone suggest a simple way to do this or suggest a better way of giving the applicant a way to access their own application and no-one elses after they have submitted their form?
    Thanks all.
    Tony Babb

    Thanks so much, that was very helpful. I added the code you suggested to create and display the random number - I called it "vollink" and that worked fine. Then I added the hidden field toward the bottom of the form - it shows at line 311 when I do a "View Source in Int Explorer and then tried adding the code to add it to the table and when I tested it failed with "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1" . The test version of the page is here www.hollisterairshow.com/volunteerapp2.php . The changes I made to add it to the table is shown below , I must be missing something blindingly obvious, if you could suggest a fix I'd really appreciate it. I did add the field to the MySQL table also.
    Thanks again
    Tony
    $editFormAction = $_SERVER['PHP_SELF'];
    if (isset($_SERVER['QUERY_STRING'])) {
      $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
      $insertSQL = sprintf("INSERT INTO volunteers (firstname, lastname, email, thursday, friday, saturday, sunday, monday, activity, talents, specialrequests, tshirt, phone, street, city, st, zip, updatedby, vollink) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, $s)",
                           GetSQLValueString($_POST['firstname'], "text"),
                           GetSQLValueString($_POST['lastname'], "text"),
                           GetSQLValueString($_POST['email'], "text"),
                           GetSQLValueString($_POST['thursday'], "text"),
                           GetSQLValueString($_POST['friday'], "text"),
                           GetSQLValueString($_POST['saturday'], "text"),
                           GetSQLValueString($_POST['sunday'], "text"),
                           GetSQLValueString($_POST['monday'], "text"),
                           GetSQLValueString($_POST['activity'], "text"),
                           GetSQLValueString($_POST['specialtalents'], "text"),
                           GetSQLValueString($_POST['specialrequests'], "text"),
                           GetSQLValueString($_POST['tshirt'], "text"),
                           GetSQLValueString($_POST['phone'], "text"),
                           GetSQLValueString($_POST['street'], "text"),
                           GetSQLValueString($_POST['city'], "text"),
                           GetSQLValueString($_POST['st'], "text"),
                           GetSQLValueString($_POST['zip'], "text"),
            GetSQLValueString($_POST['vollink'], "text"),
                           GetSQLValueString($_POST['lastname'], "text"));
      mysql_select_db($database_adminconnection, $adminconnection);
      $Result1 = mysql_query($insertSQL, $adminconnection) or die(mysql_error());

  • UDF to generate 4-Digit Positive Random Number

    Hi,
    I want to generate 4-Digit Positive Random Number in the Mapping for a field. Could anyone please provide Inputs or a UDF.
    Regards,
    Varun

    This is the UDF I have Used
    java.util.Random randomNumber;
    String TransactionId = "" ;
    randomNumber = new Random();
    long longNumber = randomNumber.nextLong();
                              if (longNumber < 0)
                                  longNumber =  longNumber * (-1);
                               longNumber += 10000;
    TransactionId  = (String.valueOf(longNumber)).substring(0, 4);
    return TransactionId ;
    Thanks everyone for your Inputs

  • 8 digit random number

    Hi,
    Can any one help me to generate 8 digit unique random number, i have code some thing like
    new Random().nextInt(100000000) + 1, but it generating 7 digit random number some times.
    please give me the solution
    thanks,
    pvmk

    pvmk wrote:
    Is there any alternate logic to generate 8 digit random numberAs already implied
    if you want random numbers in the range [10000000;99999999]
    generate random numbers in the range [0;90000000[ and add 10000000                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Unique random

    Hi all I need algorithm to generate unique random number.

    but i need it all of them as digit number
    and i need it it unique, if i have saved it before in a
    DB , and i need to re-generate again i need unique number.I'll have to make a guess at what you mean...
    Just use a cyclic random number generator, and save the "current position" somewhere.

  • 17 digit random number

    I need to create a 17 digit random number to be used as a primary field in a database. They must all be unique. First, what is the best way to create a 17 digit random number, and second, how do I make sure it is unique?

    I have a servlet that is parsing an email distribution
    list and creating multiple users. For each user I
    need to create a user id in the form of
    "ID87890988909099900" that is unique. I do not know
    how I would get Access to do this for me.Wow, you must have a lot of users. There aren't that many ants on earth.
    Seriously, if the ID will always start with ID, then can't you just leave it off the key and add it when the ID is displayed?

  • Generating N-digit random number??

    Hi All,
    I need to generate N-digit random number, anyone can help me?
    Thx in advance b4...

    import java.util.*;
    public class Try{
         public static void main(String []args){
              for(int i=0;i<100;i++){
                    System.out.println((int)(Math.random() * 10));
    }Yes I saw the casting, no problem with it, but I did used it alot to generate me number between a range of 1 - 9
    and if you want a range of 1 - 10, i just put a
    1+(int)(Math.random() * 10)

  • 3 digit random number

    Hi All,
    While running my application,I want to generate 3digit random number.
    Is there any pre defined function.If not please suggest me some idea to get the same
    Thanks in advance

    SQL> EXEC DBMS_RANDOM.VALUE(100,999)
    BEGIN DBMS_RANDOM.VALUE(100,999); END;
    FOUT in regel 1:
    .ORA-06550: line 1, column 7:
    PLS-00306: wrong number or types of arguments in call to 'VALUE'
    ORA-06550: line 1, column 7:
    PL/SQL: Statement ignored
    SQL> select dbms_random.value(100,999) from dual;
                DBMS_RANDOM.VALUE(100,999)
    338,5994241488838095483116070509848701
    1 rij is geselecteerd.
    SQL> select trunc(dbms_random.value(100,1000)) from dual;
        TRUNC(DBMS_RANDOM.VALUE(100,1000))
                                       918
    1 rij is geselecteerd.Regards,
    Rob.

  • How can I create a random number and letter in a text field...

    Hi All,
    I am using application express. I want to get a random number and letter for a text field but not sure how to do this. Say I have a licence form and I want to get the licence number automatically when user wants to create a new licence and it needs to be unique. The format I am looking for is - HQ2631 something like this.
    I am thinking I have to create a trigger but not sure how to go about this....
    advanced thanks,
    Tajuddin

    Something to play with:
    Your method can generate 26 * 26 * 10000 different licence_ids
    Generating one million ids takes 20 seconds but produces between 70000 and 80000 duplicates and is rapidly getting worse (if that is something over 7%, doubling the ids generated that grows to something under 30%)
    Just see if you can live with that.
    select sum(collisions) all_duplicates,count(*) distinct_duplicates,max(collisions) max_multiple
      from (select licence_id,count(*) - 1 collisions
              from (select DBMS_RANDOM.STRING('',2) || trunc(DBMS_RANDOM.VALUE(1000,9999)) licence_id
                      from dual
                     connect by level <= :to_generate
             group by licence_id
             having count(*) > 1
           )Regards
    Etbin

  • Random number generation that sticks

    Hello,
    I would appreciate any help with the appropriate JavaScript to generate a random 8 digit number XXXX-XXXX on a form when the user starts to enter data. This number would need to be permanent so that it can be used as a tracking identifier later.
    Thanks for looking.
    Dawson

    If you're using Oracle8i, you can use the new SAMPLE clause in the FROM clause of the SELECT statement to query a random sample from a single table. Otherwise, just write a simple random number generator in PL/SQL (see any good algorithm book under the topic "Psuedorandom number generator"). You need to understand a bit about statistics, if being "truly" random is important, to use these tools.

  • RE: Random number keys

    I have a need to generate random number keys for a DB2/6000 database. I am
    expecting 10000 new entries per day to the table and due to design
    constraints need to use an eight digit key. Also for design reasons the key
    on the database is an integer attribute. I have looked at the built in
    random number generator and it only produces a 2 byte number - I need 4.
    Does anyone have a handy routine to produce larger random numbers?
    TIA
    /\/\ark /\/ichols
    "Focus on the QUESTION, not on the ANSWER."
    Lee Wei of Forte contributed a demo called RandomSort to Vol 1. of
    the FShare CDROM. This has some generation code based on the Sedgewick LC
    algorithm that you could probably adapt to your purposes. (e.g. I generated
    8-digit integer values by multiplying Lee Wei's default for MAX by 10).
    You can fiddle with the thing to change the minimum end of the range.
    Robert Sedgewick's Algorithms in C (Addison Wesley 1990) is useful to
    have for this sort of amusement.
    If you don't have access to the FShare 1 CDROM I could send you the
    pex file.
    Regards,
    Stephen Porterfield
    Longs

    There is an excellent book written by Knuth about
    Randon Numbers generator algorithms...
    "The Art of Computer Programming, Donald E. Knuth" -- Vol 2, pp 1-170
    Have a look at these other books if you want more info:
    "Monte Carlo Simulations: Hidden Errors from 'Good' Random Number
    Generators",Physical Review Letters, Alan M.
    Ferrenberg, D. P. Landau, and Y. Joanna Wong, Vol 69 No. 23, Dec 7 1992
    "Monkey Tests for Random Number Generators",Computers Mathematics
    Applications, G Marsaglia, and A Zaman, Vol 26, No
    9 1993, pp 1-10.
    A Current View of Random Number Generators", Computer Science and
    Statistics: 16th Sympossium on Interface, Elsevier,
    1985.
    Hope this helps,
    --francois
    From: Vasas, Marty
    Sent: Thursday, April 10, 1997 8:31 AM
    To: mark h. nichols; forte users
    Subject: RE: Random number keys
    16
    Let's write 2 as twoToThe16th
    If there's nothing built-in to do the job, write a method for the
    following algorithm:
    generate 2 two-byte integers, A and B, from the built-in random number
    generator.
    return A * twoToThe16th + B
    From: mark h. nichols
    Sent: Thursday, April 10, 1997 10:10 AM
    To: forte users
    Cc: vasasm; murthis
    Subject: Random number keys
    MCI Mail date/time: Thu Apr 10, 1997 7:25 am EST
    Source date/time: Thu, 10 Apr 1997 06:37:17 -0500
    I have a need to generate random number keys for a DB2/6000 database. I
    am
    expecting 10000 new entries per day to the table and due to design
    constraints need to use an eight digit key. Also for design reasons the
    key
    on the database is an integer attribute. I have looked at the built in
    random number generator and it only produces a 2 byte number - I need 4.
    Does anyone have a handy routine to produce larger random numbers?
    TIA
    /\/\ark /\/ichols
    "Focus on the QUESTION, not on the ANSWER."

  • Random number keys

    I have a need to generate random number keys for a DB2/6000 database. I am
    expecting 10000 new entries per day to the table and due to design
    constraints need to use an eight digit key. Also for design reasons the key
    on the database is an integer attribute. I have looked at the built in
    random number generator and it only produces a 2 byte number - I need 4.
    Does anyone have a handy routine to produce larger random numbers?
    TIA
    /\/\ark /\/ichols
    "Focus on the QUESTION, not on the ANSWER."

    There is an excellent book written by Knuth about
    Randon Numbers generator algorithms...
    "The Art of Computer Programming, Donald E. Knuth" -- Vol 2, pp 1-170
    Have a look at these other books if you want more info:
    "Monte Carlo Simulations: Hidden Errors from 'Good' Random Number
    Generators",Physical Review Letters, Alan M.
    Ferrenberg, D. P. Landau, and Y. Joanna Wong, Vol 69 No. 23, Dec 7 1992
    "Monkey Tests for Random Number Generators",Computers Mathematics
    Applications, G Marsaglia, and A Zaman, Vol 26, No
    9 1993, pp 1-10.
    A Current View of Random Number Generators", Computer Science and
    Statistics: 16th Sympossium on Interface, Elsevier,
    1985.
    Hope this helps,
    --francois
    From: Vasas, Marty
    Sent: Thursday, April 10, 1997 8:31 AM
    To: mark h. nichols; forte users
    Subject: RE: Random number keys
    16
    Let's write 2 as twoToThe16th
    If there's nothing built-in to do the job, write a method for the
    following algorithm:
    generate 2 two-byte integers, A and B, from the built-in random number
    generator.
    return A * twoToThe16th + B
    From: mark h. nichols
    Sent: Thursday, April 10, 1997 10:10 AM
    To: forte users
    Cc: vasasm; murthis
    Subject: Random number keys
    MCI Mail date/time: Thu Apr 10, 1997 7:25 am EST
    Source date/time: Thu, 10 Apr 1997 06:37:17 -0500
    I have a need to generate random number keys for a DB2/6000 database. I
    am
    expecting 10000 new entries per day to the table and due to design
    constraints need to use an eight digit key. Also for design reasons the
    key
    on the database is an integer attribute. I have looked at the built in
    random number generator and it only produces a 2 byte number - I need 4.
    Does anyone have a handy routine to produce larger random numbers?
    TIA
    /\/\ark /\/ichols
    "Focus on the QUESTION, not on the ANSWER."

  • Images loaded with a random number

    Hi All,
    I have made a flash movie as seen at
    http://www.coffeemamma.com.au
    and would
    like to change the following:
    I'd like to generate three random numbers from 1 to 5
    inclusive but I want
    to ensure that each number is different - e.g. 2, 4, 1 (not
    2, 4, 2). I know
    how to generate ONE random number, but I'm stuck on comparing
    them to see
    whether they are the same (and if they are, then generate new
    numbers until
    they are unique).
    With the three numbers I would like to load images based on
    those numbers -
    e.g. '_image_2.jpg' then '_image_4.jpg' then '_image_1.jpg'.
    This part is
    fine IF I can generate the numbers.
    I'd also like to have the images come to the front when they
    are hovered
    over with the mouse and then to go 'back' to their original
    position when
    the mouse moves away. This I'm completely stuck on.
    I'd also like to be able to put the newly loaded images into
    certain
    positions (as per the example) rather than only loaded to
    (0,0) coordinates.
    I also want to be able to have the images masked as they come
    in (as per the
    example) in order to avoid white corners on the top images.
    I also have a page as per
    http://www.wasabi.org.au/wodonga.shtml
    which has
    some of the functions I want to use, but I can't seem to get
    some of the
    functions working in my new movie...
    Many thanks,
    Bruce

    I recommend to read      at least the first two "introduction articles" from sun's Java >Card homepage: http://developers.sun.com/techtopics/mobility/javacard/articles/
    Thanks a lot ... really interesting. I learnt already alot with that !
    I am sorry but the code you posted is J2SE-code, not JavaCard code. The JavaCard >environment is very restricted:
    No Integer (most cards don't even support the simple "int") and no String class.Oops :) ... i knew something was wrong hi hi
    This is my new code, it seems to work:
        /* SEND_CHALLENGE */
        private void sendChallenge(APDU apdu){
        byte[] apduBuffer = apdu.getBuffer();
        RandomData random = RandomData.getInstance(RandomData.ALG_SECURE_RANDOM);
        random.setSeed(randomArray,(short)0,(short)32);
        random.generateData(randomArray,(short)0, (short)32);
        Util.arrayCopy(randomArray,(short)0,apduBuffer,(short)0,(short)32);
        apdu.setOutgoingAndSend((short)0, (short)32);
        }Thanks a lot
    I'am going to be back soon for some more questions i think !

  • Array of unique random numbers - help?

    basically, i'm trying to create a BINGO card.
    i stared learning Java a week ago and am finding it easy to learn and use....at least so far.
    my instructor gave me the assignment to created a BINGO card. so i used the system.out.format command to make 5 tab fields, 8 spaces each ("%8s %8s %8s %8s %8s", "B", "I", "N", "G", "O"). although the assignment only wanted me to think numbers out of thin air, just to use as examples, i went above and beyond with the idea to make a BINGO card generator that could actually function.
    then i started the random number sequences, using the Math.random() command. all told, there are 24 of these in the program, one for each number on a bingo card. the field in the middle is the FREE space, so it has no Math.random command.
    in BINGO, each letter (one of my five tab fields) can have a number in ranges of 15. B is 1 to 15, I is 16 to 30, etc. it looks similar to this:
    B I N G O
    9 19 39 57 66
    3 28 32 51 74
    3 29 FREE 46 70
    14 28 43 55 67
    9 24 35 59 62
    as you can tell, i'm having trouble with actually making unique random numbers so that none repeat.
    is there a command or string or something to help me accomplish this?

    The best way I've come up with is to use an object to store the numbers that implements Collection--like ArrayList...
    Then you load it with the range of numbers that you want and call, shuffle() on the object, that will randomize your range of numbers and then you can choose the quantity you want from the storage object. So let's say you need 25 number in the range of 1 to 100:
    Add the numbers (you have to use classes so I would do Integers) 1 to 100;
    call shuffle()
    pull back the first 25 numbers
    This will guarantee that each number is distinct and in a random order.
    If you need multiple sheets populated, you can just call shuffle() between population of each sheet.
    package Junk;
    import java.util.ArrayList;
    import java.util.Collections;
    class Junk{
      private ArrayList<Integer> l;
      Junk(){
        l = new ArrayList<Integer>();
      public void loadList(int s, int e){
        for(int i=s; i<=e; i++){
          l.add(new Integer(i));
      public void randomizeList(){
        Collections.shuffle(l);
      public void printList5(){
        for(int i=0; i<5; i++){
          System.out.println(l.get(i));
      public static void main(String[] args){
        Junk j = new Junk();
        j.loadList(10,99);
        j.randomizeList();
        j.printList5();
        System.out.println();
        j.randomizeList();
        j.printList5();
    }

  • Generate random number and letter in a text field ...

    Hi All,
    I having a bit trouble when trying to generate random number and letter in application express in a text field. Say I have a licence form and when user wants to create a licence, it will provide a random licence number in the licence number field but it has to be unique. the number should be something like HQ2345. I was trying to set the following sql in default value for that text field but it is not working -
    select DBMS_RANDOM.STRING('',2) || trunc(DBMS_RANDOM.VALUE(1000,9999))
    from dual;
    any suggesion !!!!
    thanks

    Put this as default value for your text-item
    DBMS_RANDOM.STRING('',2) || trunc(DBMS_RANDOM.VALUE(1000,9999))
    with type PL/SQL Expression. It have tested it and it works fine.
    DickDral

Maybe you are looking for

  • Message Status of Sent Messages Is Opened

    I have a an intermittent problem when I send messages.  Occasionally when I send a message from Outlook, the sent message synchronizes with my BB with a Message Status of "Openened".  The majority of the time, when I send a message from Outlook, it s

  • Illustrator CS6 "side-by-side configuration error"

    Hi, When I try to run Illustrator, it would give me an error message that says " C:\Program Files\ Adobe\Adobe Illustrator CS6 (64 Bit)\Support Files\Contents\ Windows\Illustrator.exe The application has failed to start because its side-by-side confi

  • Networking Career

    Hi Friends, I am having a question regarding the pros & cons of Network Support Vs Network Testing field in IT Industry.Having 6+ yrs of experince in Netwroking field and currently working with testing profile.Holding CCNP & CCIP (MPLS&BGP) Certifica

  • Problems with cirrus logic drivers. bootcamp 3.2. osX 10.6.6. win XP

    got the soundproblems with the cirrus logic audio drivers as stated several times in the forum. latest version of osX and bootcamp. win xp sp3. tried 3 installations... mic creates the "darth vader" voice. this can be fixed with this hacked driver fr

  • My new ipad is stuck with the itunes logo on it

    My Mother got a new ipad2 for christmas. was getting it started for her and it is now stuck on the itunes screen with logo and a cord.