Results 1 to 4 of 4

Thread: Generate unique four digit number

  1. #1
    Join Date
    Aug 2004
    Location
    Goa
    Posts
    38

    Generate unique four digit number

    Hi friends,

    I want to generate four digit unique number which i can give it to my employees everyday because everyday whenever an employee comes in the office he gets a new four digit PIN number which he has to use it while accessing any computer.So is there any code where i can generate four digit unique number each and every time.

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: Generate unique four digit number

    Use the following code for creating four digit unique number.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>                              
    
    int main(void)
    
    {
    int i, n=4, p, q, r,s;    
    
    p = rand() % 10;   /* First digit can be any within the set */
    q = rand() % 9;    /* Second digit can be any except one (p) */
    
    /* If p == q, it's safe to add 1 to q, because q's initial value is 0-8 */
    if( q == p )
      q++;
    
    r = rand() % 8;    /* Third digit can be any except two (a or q) */
    
    if( ( r == p ) || ( r == q ) )
    {
      r++;
      if( ( r == p ) || ( r == q ) )
        r++;
    }
    
    /* More of the same for s. Check against p, q, and r for uniqueness */
    s = rand() % 7;    /* Fourth digit can be any except three (p, q, or r) */
    if( ( s == p ) || ( s == q ) || ( s == r ) )
    {
      s++;
      if( ( s == p ) || ( s == q ) || ( s == r ) )
      {
        s++;
        if( ( s == p ) || ( s == q ) || ( s == r ) )
          s++;
      }
    }

  3. #3
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Generate unique four digit number

    Use the following code to create four digit number in Java


    Code:
    public class RandomNumber {
    
    public static void main(String[] args) {
    
    // Take a random number who's range will be
    // 0 - 1 and multiply it by 8847. This
    // will give you an double and then you
    // can cast it to an int.
    int value = (int)(Math.random() * 8847);
    
    System.out.println(value);
    }
    }

  4. #4
    Join Date
    Aug 2010
    Posts
    2

    idea Re: Generate 10 Digit unique number on Date

    Use the following code for creating 10 digit unique number on the basis of dates. It is unique in seconds level not design for milliseconds.
    this program will create unique numbers upto 52 years, reset it with changing 'adder' value.

    Author : Sunil Wagh (SCJP, SCWCD, OCP , IBM Maximo 7 Certified)
    --------------------------------------------------------------------------
    import java.text.DecimalFormat;
    import java.util.Calendar;

    public class Demo2 {
    // truncate four digit to two digit 2010=>10
    public static int retval(int y){
    String yrs=""+y;
    yrs=yrs.substring(1);
    y = Integer.parseInt(yrs) ;
    return y;
    }
    // Create 10 Digit Unique number
    public void test(){
    int adder = 55;//Number Start from year 2010, change 64 to start from 2001
    Calendar cal = Calendar.getInstance();
    int year = cal.get(Calendar.YEAR);
    int doy = cal.get(Calendar.DAY_OF_YEAR);
    int hr = cal.get(Calendar.HOUR_OF_DAY);
    int mi = cal.get(Calendar.MINUTE);
    int ss = cal.get(Calendar.SECOND);
    int yr = adder+retval(year);
    char c=(char)yr;
    DecimalFormat df = new DecimalFormat("000");
    System.out.println("Unique Num: " + c + df.format(doy) + hr + mi + ss);
    }

    public static void main(String a[]){
    Demo2 d = new Demo2();
    d.test();
    }
    }
    -------------------------------------------------------------------------
    Output : Unique Num: A23117587
    ------------------------------------------------------------------------
    Last edited by SunilWagh; 19-08-2010 at 06:38 PM.

Similar Threads

  1. Adding digit in front of number in cell in Microsoft Excel
    By Thedevotee in forum MS Office Support
    Replies: 2
    Last Post: 07-02-2012, 07:45 PM
  2. Replies: 2
    Last Post: 01-02-2012, 07:10 PM
  3. Replies: 4
    Last Post: 13-01-2012, 05:07 PM
  4. Generate 10 Digit unique number
    By SunilWagh in forum Software Development
    Replies: 1
    Last Post: 20-08-2010, 12:17 PM
  5. 4-Digit Number Code (0-9)
    By christheart in forum Off Topic Chat
    Replies: 3
    Last Post: 03-07-2009, 10:32 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,250,983.63287 seconds with 17 queries