Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
How to auto generate number between 10000 to 99999 without repeat number
Posted 10-Apr-13 21:31pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Create random numbers between the give range using the Random class.
Then put the number in a dictionary.
Generate the next number.
If the number is present in the dictionary create a new number else display this number.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

From your question its not clear that where you want this means in code behind or database query.
 
if want in code-behind try this:-
Random number = new Random();
 
int genNumber = number.nextInt();
 
while (true)
 {
    genNumber = number.nextInt();
    if (genNumber > 10000 && genNumber < 99999)
                    {
        break;
      }
}
System.out.println("Random  Number:"+genNumber);
 

sql query:-
 
SELECT FLOOR(RAND() * 99999) AS random_num
FROM table_Name
WHERE "col_Name" NOT IN (SELECT my_number FROM numbers_mst)
LIMIT 1
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Why just not use standard Random.Next Method (Int32, Int32)[^]?
Random rand = new Random();
int value = rand.Next(10000, 100000);
In case you are generating random numbers in loop, you need to create instance of Random outside the loop (class static readonly field would be good for this).
 
In SQL Server you can use this code for getting single random number:
SELECT 10000 + CAST(RAND() * (100000 - 10000) AS INT)
In case you need to generate random number for each row of your result set, you have to use NEWID[^] instead of RAND[^] function:
SELECT 10000 + (ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) % (100000 - 10000))
  Permalink  
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 195
1 ProgramFOX 130
2 Maciej Los 105
3 Afzaal Ahmad Zeeshan 82
4 Sergey Alexandrovich Kryukov 75
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 11 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100