Discussion Locked

random number generation

0 helpful
4 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I need a small program that will generate random numbers. Criteria is: use the numbers 1-5, must be 6 characters long, no less, no more, no identical values, and no more than two occurances in a row.
Examples are: 123451 and 123451 (identical values) is bad. 434345 is good, 124445 is bad (more than two occurances in a row).
This needs to be done in some type of common format such as Excel, or as a stand alone program. It is required to print the values only, (prefer multiple columns per sheet,) but also viewing the values is better.

4 replies

by bojidar alexandrov

Const CountOfNumbers = 500
Const CountOfRows = 5

Private Function GetRandomDigit1_5() As Integer
GetRandomDigit1_5 = Int((5 * Rnd) + 1)
End Function

Private Function GetRandomNumber() As Long
Dim i As Integer
Dim LastDigit As Integer, Repetitions As Integer
Dim CurrentDigit As Integer
Dim Number As String

LastDigit = 0
Repetitions = 0
Number = ""

i = 1
While i <= 6
CurrentDigit = GetRandomDigit1_5()
If CurrentDigit = LastDigit Then
Repetitions = Repetitions + 1
Else
Repetitions = 0
LastDigit = CurrentDigit
End If

If Repetitions < 2 Then
Number = Number + CStr(LastDigit)
i = i + 1
End If
Wend

GetRandomNumber = CLng(Number)
End Function

Public Sub GenerateRandomNumbers()
Dim i As Long
Dim CurrentRow As Integer

Randomize

For i = 1 To CountOfNumbers \ CountOfRows For CurrentRow = 1 To CountOfRows
Sheet1.Cells(i, CurrentRow) = GetRandomNumber
Next
Next
End Sub

This discussion is closed to new replies.