Need a unique random number generator! Help please!

Jessicalucy14

New member
Joined
Feb 14, 2010
Messages
1
Programming Experience
Beginner
Hi,

For part of my coursework I need to be able to enter some customer details into a form and then to press a button which give that customer a unique reference number.

I've tried to find a solution for days and cant find anything which would give me any idea how to do it??

Anyone got any ideas?

Thanks in advance!
 
It seems to me that you should be able to assign a sequential number to each new customer, so it doesn't have to be random. If you really do need a random number, you also need to decide on a range. Then you can use the Shuffle Sort algorithm so none of the numbers are repeated. Here is a demo Console application you can use to see how it works.


VB.NET:
Module Module1

    Sub Main()
		Dim snum, entry As String, low, high, range, x, mix, temp As Integer
		Console.WriteLine("Shuffle Sort - Random numbers without duplicates")
		Console.Write("Enter lowest number in range:  ")
		snum = Console.ReadLine()
		Integer.TryParse(snum, low)
		Console.Write("Enter highest number in range:  ")
		snum = Console.ReadLine()
		Integer.TryParse(snum, high)
		range = high - low + 1
		Dim rndarray(range) As Integer
		Dim randnum As New Random()
		For x = 1 To range
			rndarray(x) = x + low - 1
		Next x
		Console.WriteLine()
		Console.Write(range & " random numbers w/o duplicates.  ")
		Console.WriteLine("Enter to repeat, any other key to stop." & vbCrLf)
		Do
			For x = 1 To range
				mix = randnum.Next(x, range)
				temp = rndarray(mix)
				rndarray(mix) = rndarray(x)
				rndarray(x) = temp
			Next x
			For x = 1 To range
				Console.Write(rndarray(x) & " ")
			Next x
			Console.WriteLine(vbCrLf)
			entry = Console.ReadKey(True).KeyChar
		Loop While entry = Chr(13)
    End Sub

End Module
 
I would say it would be easier to get a shuffled List(Of Integer) and then pull a number from the 0 index each time a user requests a new number.

VB.NET:
    Sub Main()
        Dim low = GetLowNumber()
        Dim high = GetHighNumber(low)
        Dim shuffledList = GetListOfInteger(low, high)
        UniqueOutput(shuffledList)
    End Sub

    Private Function GetLowNumber() As Integer
        Dim low As Integer
        Try
            Console.Write("Enter lowest number in range: ")
            low = Integer.Parse(Console.ReadLine())
        Catch ex As Exception
            Console.WriteLine("Please Enter a Number")
            GetLowNumber()
        End Try

        Return low
    End Function

    Private Function GetHighNumber(ByVal low As Integer) As Integer
        Dim high As Integer
        Try
            Console.Write("Enter highest number in range: ")
            high = Integer.Parse(Console.ReadLine())
            If low >= high Then
                Console.WriteLine("High value must be greater than low value")
                GetHighNumber(low)
            End If
        Catch ex As Exception
            Console.WriteLine("Please Enter a Number")
            GetHighNumber(low)
        End Try

        Return high
    End Function

    Private Function GetListOfInteger(ByVal low As Integer, ByVal high As Integer) As List(Of Integer)
        Dim elementCount As Integer = (high - low + 1)
        Dim rand As New Random
        Return Enumerable.Range(low, elementCount).OrderBy(Function() rand.Next()).ToList
    End Function

    Private Sub UniqueOutput(ByVal numList As List(Of Integer))
        Dim keyInput As String
        Console.WriteLine("Press Enter to output a unique number in range.")
        Console.WriteLine("Press any other key to stop." & Environment.NewLine)
        Do
            Console.WriteLine(numList.Item(0).ToString() & Environment.NewLine)
            numList.RemoveAt(0)
            keyInput = Console.ReadKey(True).KeyChar
        Loop While keyInput = Chr(13) AndAlso numList.Count > 0
    End Sub
 
VB.NET:
Dim myInt As Integer = -1
Integer.TryParse(Console.ReadLine(), myInt)

What happens to myInt when the user enters 'a' at the command line?

Though the TryParse will handle the bad input setting myInt to 0 is unintended behavior and you'll still need a Try-Catch block to handle it.
 
No you don't. Just place the code inside a Do loop:


Do
Console.Write("Enter lowest number in range: ")
snum = Console.ReadLine()
Integer.TryParse(snum, low)
Loop While low = 0 And snum <> "0"
 
VB.NET:
Dim myInt As Integer = -1
Integer.TryParse(Console.ReadLine(), myInt)
and you'll still need a Try-Catch block to handle it.
No. A simple "if then" would be enough.

Or the short version from above:
VB.NET:
Dim i As Integer
        Do
            Console.WriteLine("Enter a positive number from 1 to 10")
        Loop Until Integer.TryParse(Console.ReadLine(), i) AndAlso (i > 0 And i < 11)
 
Hi again, guys.

I've jsut finished a Real (repeat, Real) Random numbers generator, you can get it on my homepage HomeDimension - Home
The generator is successfully tested on various Celeron processors, but Pentium 4 has failed. Still, if anybody wishes to try it, please do, and let me know which other processors are up to it.

enjoy ;)
 
Hi again, guys.

I've jsut finished a Real (repeat, Real) Random numbers generator, you can get it on my homepage HomeDimension - Home
The generator is successfully tested on various Celeron processors, but Pentium 4 has failed. Still, if anybody wishes to try it, please do, and let me know which other processors are up to it.

enjoy ;)

The Framework can produce numbers random enough to be used in cryptography, so anything any more random than that is not significantly better.

If all you need is a unique ID then you should probably just use a GUID. It's not guaranteed to be unique but is so unlikely to be a duplicate that it's used in systems all over the world.

If you want your own format then use RandomNumberGenerator.GetBytes and process them however you want. The only way to be sure it's unique is to compare it to all existing values though, which is why a GUID is most often used.
 
The Framework can produce numbers random enough to be used in cryptography, so anything any more random than that is not significantly better.

If all you need is a unique ID then you should probably just use a GUID. It's not guaranteed to be unique but is so unlikely to be a duplicate that it's used in systems all over the world.

If you want your own format then use RandomNumberGenerator.GetBytes and process them however you want. The only way to be sure it's unique is to compare it to all existing values though, which is why a GUID is most often used.

Yes, yes, i know the framework is great. I also know that some people spend thousands on RNG hardware. Why? However random the Framework can go, it is not Real random, i believe. My assembly is, if it's not useful to you, you can of course use the Framework, I do that on most occasion! Because I don't develop gambling or military.
 
Back
Top