Rand() is not Ramdom

denismccarthy

Board Regular
Joined
Dec 30, 2006
Messages
108
I have a macro that picks a random team ( so I was hoping), but everytime I open the spreadsheet, the same team appears. It's suppose to pick a random number in the a drop down list.
Am I using the rand() function wrong?

Heres the line

x = Rnd() * (PlayerNo(i) - 1) + 1
x = rnd()* (38 -1)+1
x = 14
Any help would be appreciated


Sub QuickPick()


Dim x As Integer
Dim i As Integer
ReDim PlayerNo(11) As Integer
ReDim Player(11) As String
Dim Player1 As String
Dim Player2 As String
Dim Player3 As String
Dim Player4 As String
Dim Player5 As String
Dim Player6 As String
Dim Player7 As String
Dim Player8 As String
Dim Player9 As String
Dim Player10 As String
Dim Player11 As String
Dim PlayerNo1 As Double
Dim PlayerNo2 As Double
Dim PlayerNo3 As Double
Dim PlayerNo4 As Double
Dim PlayerNo5 As Double
Dim PlayerNo6 As Double
Dim PlayerNo7 As Double
Dim PlayerNo8 As Double
Dim PlayerNo9 As Double
Dim PlayerNo10 As Double
Dim PlayerNo11 As Double
Dim c As Integer

PlayerNo(1) = 19
PlayerNo(2) = 116
PlayerNo(3) = 116
PlayerNo(4) = 116
PlayerNo(5) = 116
PlayerNo(6) = 127
PlayerNo(7) = 127
PlayerNo(8) = 127
PlayerNo(9) = 127
PlayerNo(10) = 80
PlayerNo(11) = 80

Player(1) = "Goalkeeper"
Player(2) = "Defender1"
Player(3) = "Defender2"
Player(4) = "Defender3"
Player(5) = "Defender4"
Player(6) = "Midfielder1"
Player(7) = "Midfielder2"
Player(8) = "Midfielder3"
Player(9) = "Midfielder4"
Player(10) = "Striker1"
Player(11) = "Striker2"

Reset:
Application.Run "Macro2"
For i = 1 To 11

RestartPick:
x = Rnd() * (PlayerNo(i) - 1) + 1
If x > PlayerNo(i) Then GoTo RestartPick
Sheets("Form").Select
Worksheets("Form").Shapes("Drop Down " & i).OLEFormat.Object.Value = x + 1
If i >= 2 Then
For c = 1 To (i - 1)
If Worksheets("Form").Shapes("Drop Down " & i).OLEFormat.Object.Value = Worksheets("Form").Shapes("Drop Down " & c).OLEFormat.Object.Value Then
GoTo RestartPick
Else
End If
Next
Application.Run Player(i)

If WorksheetFunction.Max(Sheets("PrintForm").Range("k1:k40")) > 3 Then
GoTo RestartPick
Else
End If
Else
End If
Application.Run Player(i)
Next

Range("i6").Select
If Selection < 0 Then GoTo Reset
Range("c4").Select
End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Hi

You need to use the Randomize statement at the top of your code to create a new seed value:

Code:
Sub QuickPick() 

Randomize

'blah blah blah...
 
Upvote 0

Forum statistics

Threads
1,217,673
Messages
6,137,916
Members
450,098
Latest member
Ikmal Sabri

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top