Random Number Generating

G

Guest

I need a program that generate 5 non-duplicates random number between 1-10
as string values store in an array.

Do anybody know of any good books or websites that explain how to generator
random numbers using asp.net?

Plus If you have any coding practice ideas for the above defined project
please share them.

Thanks!
 
J

John Baker

Leon said:
I need a program that generate 5 non-duplicates random number between 1-10
as string values store in an array.

Do anybody know of any good books or websites that explain how to generator
random numbers using asp.net?

Plus If you have any coding practice ideas for the above defined project
please share them.

This is more of an algorithm question than a language question. The
non-duplicate requirement is the only reason why you can't just use the
built-in generator. But this is how I would do it.

I would make a class with the following integer members:
RandomNumber
SortNumber

Then I'd make an array of 10 instances of that class. Use a for loop to
do 10 interations, setting RandomNumber to the i-th iteration of the
loop, and SortNumber to a random number between 0 and some "large"
number. Then sort the array by SortNumber, and pull off the 5 first
elements to get your 5 non-duplicate random numbers between 1 and 10.
 
L

Leon

Thank you for your responses!

John Baker said:
This is more of an algorithm question than a language question. The
non-duplicate requirement is the only reason why you can't just use the
built-in generator. But this is how I would do it.

I would make a class with the following integer members:
RandomNumber
SortNumber

Then I'd make an array of 10 instances of that class. Use a for loop to
do 10 interations, setting RandomNumber to the i-th iteration of the loop,
and SortNumber to a random number between 0 and some "large" number. Then
sort the array by SortNumber, and pull off the 5 first elements to get
your 5 non-duplicate random numbers between 1 and 10.
 
J

Jay B. Harlow [MVP - Outlook]

Leon,
In addition to John's suggestion I would consider using something like:

Dim r As New Random
Dim list(4) As String
Dim index As Integer = 0
Do Until index >= values.Length
Dim s As String = r.Next(1, 11).ToString()
If Array.IndexOf(list, s, 0, index) = -1 Then
list(index) = s
index += 1
End If
Loop

I've also heard about a solution where you create an array with your full
range, then using a random index value into it to get the values, something
like:

Dim list(4) As String
Dim r As New Random
Dim values As New ArrayList
For valueIndex As Integer = 1 To 10
values.Add(valueIndex.ToString())
Next
For index As Integer = 0 To list.Length - 1
Dim valueIndex As Integer = r.Next(values.Count)
list(index) = DirectCast(values(valueIndex), String)
values.RemoveAt(valueIndex)
Next

Hope this helps
Jay
 
L

Leon

Thanks!

Jay B. Harlow said:
Leon,
In addition to John's suggestion I would consider using something like:

Dim r As New Random
Dim list(4) As String
Dim index As Integer = 0
Do Until index >= values.Length
Dim s As String = r.Next(1, 11).ToString()
If Array.IndexOf(list, s, 0, index) = -1 Then
list(index) = s
index += 1
End If
Loop

I've also heard about a solution where you create an array with your full
range, then using a random index value into it to get the values,
something like:

Dim list(4) As String
Dim r As New Random
Dim values As New ArrayList
For valueIndex As Integer = 1 To 10
values.Add(valueIndex.ToString())
Next
For index As Integer = 0 To list.Length - 1
Dim valueIndex As Integer = r.Next(values.Count)
list(index) = DirectCast(values(valueIndex), String)
values.RemoveAt(valueIndex)
Next

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top