# Random numbers.

This is a discussion on Random numbers. within the Excel Questions forums, part of the Question Forums category; Hi, I want to generate combintions of Random numbers based on certain criteria's. The first criteria is that the random ...

1. ## Random numbers.

Hi,

I want to generate combintions of Random numbers based on certain criteria's.

The first criteria is that the random numbers should be between 1 to 80.

Secondly, the numbers should be whole not in decimals.

Lastly, in one combination only 10 numbers should be displayed which should not be duplicated.

for ex: 1,5,22,40,5,61,68,72,78,80 is not the correct combination as 5 is duplicated in this combination of 10 numbers.

The right combination would be 1,5,22,40,42,61,68,72,78,80.

Is this possible?

Thanx

Rakesh

2. Code:
```Option Explicit

Sub RandomNumber()

Dim Choice(1 To 10) As Integer
Dim ChoiceTemp      As Integer
Dim x               As Integer
Dim y               As Integer
Dim Choices         As String
Dim Repeat          As Boolean

For x = 1 To 10
a:
Randomize
Repeat = False
ChoiceTemp = Int((80 + 1 - 1) * Rnd + 1)
For y = 1 To 10
If Choice(y) = ChoiceTemp Then
Repeat = True
Else
End If
Next y
If Repeat = True Then
GoTo a:
Else
Choice(x) = ChoiceTemp
End If
Next x
For x = 1 To 10
Choices = Choices & vbCrLf & Choice(x)
Next x
MsgBox Choices

End Sub```

3. ## Re: Random numbers.

Hi Jacob,

Thanx a million. The code that you provided works. However, what I get is a box which displays the random numbers. Is there a way that these numbers could be inserted in the worksheet directly?

Thanks again

Rakesh

4. Try this:

Code:
```Option Explicit

Sub RandomNumber()

Dim Choice(1 To 10) As Integer
Dim ChoiceTemp      As Integer
Dim x               As Integer
Dim y               As Integer
Dim Choices         As String
Dim Repeat          As Boolean

For x = 1 To 10
a:
Randomize
Repeat = False
ChoiceTemp = Int((80 + 1 - 1) * Rnd + 1)
For y = 1 To 10
If Choice(y) = ChoiceTemp Then
Repeat = True
Else
End If
Next y
If Repeat = True Then
GoTo a:
Else
Choice(x) = ChoiceTemp
End If
Next x
Range("A65536").End(xlUp).Offset(1, 0).Select
For x = 1 To 10
ActiveCell.Value = Choice(x)
ActiveCell.Offset(0, 1).Select
Next x

End Sub```

5. ## Re: Random numbers.

Hey Jacob.

One other quirky little change...
Instead of producing keno numbers, how about a lottery where you chose 6 numbers between 6 & 49? I've managed to puzzle out the number of repeats & the highest possible number (not bad for a VB pre-novice ) but can't grasp how you would set your starting number (6). Any insight?

6. Replace

ChoiceTemp = Int((80 + 1 - 1) * Rnd + 1)

with

Highest = 50
Lowest = 1
ChoiceTemp = Int((Highest + 1 - Lowest) * Rnd + Lowest)

To get 6 choices just use For x = 1 to 6

7. ## Re: Random numbers.

Great. Another lesson learned.

Thanks.

Take Care

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•