seco

Hi

i want to generate unique 8 digit number that never duplicated.
how

thanks in advance.



Re: Visual Basic Express Edition how to generate unique 8 digit number

Ryan Lamansky / Kardax

Impossible. If you're limiting to 8 digits, you only have 100 million possible values. After you've generated all of them, duplication will start.

-Ryan / Kardax





Re: Visual Basic Express Edition how to generate unique 8 digit number

js06

I'm guessing that you just want to know how to not generate a duplicate number untill you have exhausted all of them.

I'm thinking that you would have to store each number in a database and check if the current number has been generated or not.

If it hasn't then you would need to add that number to the database.

I would think that you could check for the stored numbers in the database fairly easily. I'm afraid right now i can't help with any code.

I wonder if you could do it easier by handling the 8 digits seperately instead of as a whole number (maybe)






Re: Visual Basic Express Edition how to generate unique 8 digit number

Carsten Kanstrup

Use at least a 28-bit maximum length CRC polynomial (length 2**N -1) and store and reload the contents of the polynomial when you close and open your program.





Re: Visual Basic Express Edition how to generate unique 8 digit number

seco

thanks for reply
ok what is the least length i can generate unique numbers from it and how

thanks in advance.





Re: Visual Basic Express Edition how to generate unique 8 digit number

Carsten Kanstrup

After a second thought, you must reduce the polynomial to 26 bit because it is important that all bits are included in the result to guarantee unique numbers. 2**26-1 = 67108863. This is the biggest number you can generate. If you need to get closer to 99999999, you can combine more polynomials like (2**X - 1)(2**Y - 1) etc., but then it of course gets a little tricky. With X = 25 and Y = 2, the length becomes 100663293. This is quite close, but you need to throw 0.7% of the numbers away and take the next one. With 3 or more polynomials you can get even closer.

The CRC polynomial actually cryptize (probably wrong english word - I hope you understand - what is it called when you make something cryptic ) a counter. The easiest way to generate random, unique numbers would of course be just to make a simple counter and then cryptize the result, but as far as I know, most standard methods in .NET like DES etc. use a 64 bit or 128 bit block, so all these bits must be included in the result and this is far more than 8 digits. Therefore, you need to make your own cryptizing and CRC is the easiest way.

Sorry, but I don't understand what you mean with: "what is the least length i can generate unique numbers from it". The random number generation of a 26-bit maximum length polynomial will generate random numbers in the range 1 to 67108863. As with all CRC polynomials, you cannot generate the number 0.





Re: Visual Basic Express Edition how to generate unique 8 digit number

seco

i need code for this please if any one have





Re: Visual Basic Express Edition how to generate unique 8 digit number

Carsten Kanstrup

I am very busy right now, but I will try to find the time to help you next week.

Unfortunately, Microsoft has updated Windows XP Pro in such a way that none of my old DOS programs works any longer - not even in Windows 98 mode - and I need one of these to generate a list of maximum length polynomials of a given order - Grrrrr .

How close do you need to get to 99999999 Is 67108863 enough





Re: Visual Basic Express Edition how to generate unique 8 digit number

Solitaire

Solution that includes all numbers between 00000000 and 99999999, randomly selected:

>>>>> NOTE: This doesn't quite work. It will only generate 10,000 numbers!

I discovered this a couple of days after the original post and needed to add this comment. <<<<<

I first tried it using a single array going up to 100,000,000 but it really slowed everything down to a crawl. So I decided to use two string arrays and combine them after doing a random shuffle with each one separately. This is what I came up with, and it seems to work nicely. >>>but only for 10000 numbers<<<

(Note: I revised the code to include the leading zeros, which I'd inadvertantly omitted with the original post. I also revised the Random statement to omit the seed, but you may want to put it back in. See the note below.)

Note: You will need to save both arrays and the counter in order to generate the next unique number, bypassing the first two For loops and starting with the next count value. So you will need to revise this little demo program to save and retrieve a file with that information and bypass the first part, or just create a different program to run the list of numbers once you have them already generated and saved to a text file.

If you do use a seed argument with the Random statement (which could be any integer), the sequence will always be the same every time you start the program from the beginning. That way you don't have to save the arrays and only need to know which count you used last.

Also note: If you don't want the "00000000" included, then you will have to add a statement to test for it and skip over that value. If you don't want any number to start with "0" then you will have to test for the leftmost character in the first array and exclude all those values.

Code Snippet

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim mix1, mix2, reply, count As Integer, temp, combine As String

Dim first(10000) As String

Dim last(10000) As String

Dim rnd As Random = New Random() 'seed may be placed inside ()

For x As Integer = 0 To 9999

first(x) = x.ToString

last(x) = x.ToString

Next x

For x As Integer = 0 To 9999 'swap array positions randomly

mix1 = rnd.Next(0, 10000)

temp = first(mix1)

first(mix1) = first(x)

first(x) = temp

mix2 = rnd.Next(0, 10000)

temp = last(mix2)

last(mix2) = last(x)

last(x) = temp

Next x

count = 0

Do

first(count) = first(count).PadLeft(4, "0"c)0

last(count) = last(count).PadLeft(4, "0"c)

combine = first(count) & last(count)

TextBox1.Text = combine

reply = MessageBox.Show("Press OK to show next unique number, Cancel to stop.", "Counter = " & count, MessageBoxButtons.OKCancel)

count += 1

Loop Until reply = 2






Re: Visual Basic Express Edition how to generate unique 8 digit number

seco

thanks for reply

what is i want to include characters A-Z to the numbers to increase possible values
thanks.





Re: Visual Basic Express Edition how to generate unique 8 digit number

ReneeC

Heres how to generate a unique datatype requiring 8 bytes for the datum itself

Dim Guid as guid = guid.newguid

And that's all there is to it. It takes the worry out of being close.






Re: Visual Basic Express Edition how to generate unique 8 digit number

seco

when i try to see the generated guid to a textbox it says
guid can't converted to string !! although it's explicitly converted !!

how to show it

thanks in advance.





Re: Visual Basic Express Edition how to generate unique 8 digit number

ReneeC

Textbox.text = guid.tostring

You never need to look at a guid. There aren't made to be viewed as you will see. They are made to be unique. I convert them to a Long datatype because comparison in binary is much faster than string comparisions