469,945 Members | 1,828 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,945 developers. It's quick & easy.

Generate auto number by code but not repeated.

dear sir,
i wish to generate auto number starting from 1000 to 1000000. but each time number automatically generated no duplicate number generate assign.
thanks,
arun kumar
Oct 13 '06 #1
3 1867
Expand|Select|Wrap|Line Numbers
  1. Public Function Rand(ByVal Min As Long, ByVal Max As Long) As Long
  2.   Rand = Int((Max - Min + 1) * Rnd) + Min
  3. End Function
  4.  
Example:
Expand|Select|Wrap|Line Numbers
  1. NextCard = Rand(1, 52)
  2.  
Will pick a random number between 1 and 52.
Jul 3 '07 #2
Killer42
8,435 Expert 8TB
...Will pick a random number between 1 and 52.
There's actually a function like this in our Articles section. I know, because I wrote it. :)

To cover the second part of the question, you should allocate an array and store the numbers in it. Each time you request a number from your "generate a number between..." function, check it against the array. If it has already been used, ask for another.




Hm... I think I'll write a new version of the random-number routine which won't generate repated numbers. Stay tuned...
Jul 3 '07 #3
Killer42
8,435 Expert 8TB
Took a bit longer than planned, as I had to do some other things. But here is what I've got so far. I think it works.
Jul 3 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Laphan | last post: by
6 posts views Thread by Sebastien | last post: by
2 posts views Thread by SalimShahzad | last post: by
2 posts views Thread by G | last post: by
8 posts views Thread by Marc | last post: by
4 posts views Thread by Michiel Rapati-Kekkonen | last post: by
1 post views Thread by abhaya9999 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.