Random not very random

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,922
Office Version
  1. 365
Platform
  1. Windows
Hi,

For a bit of fun, I have a list of quotes which when my main macro is run, automatically selects and displays one. Simplified code I'm using is:

Code:
Sub Randomi ()
 
Dim i as integer
i = (i * Rnd() + 1)
MsgBox i
 
End Sub

But everytime I first run this macro after first opening the workbook, i is always generated with the same value - does anyone know why this is and how to make it not do this? I have other code that bounds i between a minimum and maximum value but as said, above is very reduced as this is where in my code the problem is occuring.

Thanks,
Jack
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Try this

Code:
Sub Randomi()
Dim i As Integer
Randomize
i = (i * Rnd() + 1)
MsgBox i
End Sub
 
Upvote 0
From the help on RND()

For any given initial seed, the same number sequence is generated because each successive call to the Rnd function uses the previous number as a seed for the next number in the sequence.

Before calling Rnd, use the Randomize statement without an argument to initialize the random-number generator with a seed based on the system timer.
 
Upvote 0
I think you have to issue a 'randomize' command first. I think the random number generator does operate along a (randomly seeded) sequence though - genuine randomness has always been an issue to the black and white world of binary coding.

HTH
 
Upvote 0
VoG thanks for the code modification,
Steve059L thanks for the explanation,
Weaver, CoG beat you to it, but thanks for your reply!

Cheers guys,
Jack
 
Upvote 0

Forum statistics

Threads
1,215,640
Messages
6,125,972
Members
449,276
Latest member
surendra75

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