Thursday, October 28, 2010

How do you make a random number generator in vb that doesn't repeat numbers and shows the number in a textbox?

All I want to know is how to make a random number generator that generates a unique random number each time i click on the randomize button. Then when a random number is generated i want it to appear in a text box. The random number should not be repeated ever.How do you make a random number generator in vb that doesn't repeat numbers and shows the number in a textbox?
textbox1.text = RandomNumber(30000)



Public Function RandomNumber(ByVal MaxNumber As Integer, _

Optional ByVal MinNumber As Integer = 0) As Integer



'initialize random number generator

Dim r As New Random(System.DateTime.Now.Millisecond)



'if passed incorrect arguments, swap them

'can also throw exception or return 0



If MinNumber %26gt; MaxNumber Then

Dim t As Integer = MinNumber

MinNumber = MaxNumber

MaxNumber = t

End If



Return r.Next(MinNumber, MaxNumber)



End FunctionHow do you make a random number generator in vb that doesn't repeat numbers and shows the number in a textbox?
The last requirement greatly complicates a solution. Unless there is unlimited precision available, eventually a number is going to be repeated. What's the range of numbers? Let's say zero to a thousand. So when a click occurs a new number is generated and displayed. And the next, etc. But what happens after the 1001st click?

No comments:

Post a Comment