Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Truly Random Number Generator 1

Status
Not open for further replies.

atruhoo

Programmer
Jan 8, 2003
131
US
New to VB.Net and looking for a true random number generator. Currently have a random number generator but at times get duplicates and that is when just generating 6 numbers. I know that in VB6 you could use Randomize, not sure if can in .Net or is there something like it. Thanks in advance for any help.

 
Randomizing Numbers will give you duplicates. If you randomly pick a number between 1 and 6 it will duplicate unless you have a function to rule out items already checked.

This will give you random numbers from 1 to 60 without getting duplicates.

It load already selected numbers into an array list and then checks to see if they have been chosen before

Sub GetRandomNumber()
Do

Dim rndNum As Random = New Random()
Dim iLine As Integer = rndNum.Next(1, 60)
If Not myList.Contains(iLine) Then
myList.Add(iLine)
'You can do whatever you want with
'the random number here
myList.TrimToSize()

End If


Loop Until myList.Count >= 60

End Sub

DotNetDoc
M.C.S.D.
---------------------------------------

Tell me and I forget. Show me and I remember. Involve me and I understand.
- Anonymous Chinese Proverb

-----------------------------------

If you can't explain it simply, you don't understand it well enough.
- A. Einstein





 
Hi,
DotNetDoc is right. However, the code he shows has one problem: when a number comes out twice it simply tries again. So when it comes to the last number it simply continues to generate random numbers until that exact number comes out (the problem is the same for all the number except the first, but worst for the last number). In fact there is no guaranty that that number ever will come out and the code could in theory loop indefinitely. The solution is to create an array with the desired numbers and then shuffle the array. That can be done in may ways, here is the simplest:
--------------------------------------------------------
Dim ShuffleIndex As Int32 = 1000
Dim n As Int32 = 60
Dim ShuffleArr(n), i, j, k, l As Int32
'populate array
For i = 0 To n
ShuffleArr(i) = i
Next
'Shuffle
Dim rndNum As Random = New Random()
For i = 0 To ShuffleIndex
'Pick 2 random items and exchange their places in the array
k = rndNum.Next(0, n)
l = rndNum.Next(0, n)
j = ShuffleArr(k)
ShuffleArr(k) = ShuffleArr(l)
ShuffleArr(l) = j
Next
--------------------------------------------------------

Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
sunaj is right. The program that I use that function on I actually have to use a Sleep on because I wanted it to pick a random number every 5 minutes. So as you can see speed was not an issue.

Nice code sunaj :)

DotNetDoc
M.C.S.D.
---------------------------------------

Tell me and I forget. Show me and I remember. Involve me and I understand.
- Anonymous Chinese Proverb

-----------------------------------

If you can't explain it simply, you don't understand it well enough.
- A. Einstein





 
Sorry took so long to get back was under the weather. Wanted to thank both of you for your input. Originally wrote code similar to DotNetDoc, but now redoing since sunaj made point about possible infinite loop.

Richard
 
Here you go. It uses the system clock for the generation.

When using " Rnd "
Label1.Text = CStr(Int(Rnd() * 10))


add the following under Form1_Load
Randomize()

Example___________________________________________

Public Class Form1
Inherits System.Windows.Forms.Form


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Randomize() 'Here is the KEY to Random
End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_ System.EventArgs) Handles Button1.Click
Label1.Text = CStr(Int(Rnd() * 10)) ' pick 1 of 10 numbers
Label2.Text = CStr(Int(Rnd() * 10))
Label3.Text = CStr(Int(Rnd() * 10))
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
End
End Sub

End Class


Good Luck,
 
Woofer2,

I belive that will give him repeted numbers. Every time you use CStr(Int(Rnd() * 10)) it will pick a number between 1 and 10 but it does not keep track of numbers it already picked. That is why an array and arraylist are used above. To keep track of what numbers were picked.

DotNetDoc
M.C.S.D.
---------------------------------------

Tell me and I forget. Show me and I remember. Involve me and I understand.
- Anonymous Chinese Proverb

-----------------------------------

If you can't explain it simply, you don't understand it well enough.
- A. Einstein





 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top