Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

50 random numbers 2

Status
Not open for further replies.

onn99

IS-IT--Management
Dec 10, 2000
24
AU
I need to create 50 random numbers and put them on a worksheet in cells (A1:J5).

random numbers must be between the value of 0 (including) and 50 (excluding).

this is to be put in a module so i can assign a button to it on my own toolbar and with a simple click of a button, bam the random numbers appear on the worksheet.

Your advise is greatly appreciated

onn99
 
Hi, it's me, onn99 again.

I forgot to mention in the email that the numbers must be random accurate 5 decimal points, otherwise there is only 50 numbers and only 50 cells hence not very random. Stupid me to forget to mention that before.

Cheers

onn99
 
Hi onn99,

This should do it for you:

Code:
With ActiveSheet.Range("a1:j5")
    .NumberFormat = "00.00000"
    .Formula = "=RAND()*50-0.00001"
End With

Enjoy,
Tony
 
If you want actual values (instead of formulas) in the cells, you can use this:
[blue]
Code:
Sub LoadRandomNumbers()
Dim c As Range
  For Each c In ActiveSheet.Range("a1:j5")
    c.NumberFormat = "0.00000"
    c.Value = Int(Rnd() * 5000000) / 100000
  Next c
End Sub
[/color]


 
A very slightly different version is

Code:
Sub a()
Dim c As Range
Dim x As Single
For Each c In Range("a1:j5")
Randomize
x = Rnd() * 50
c = Round(x, 5)
Next
End Sub

The main difference here is the use of "Randomize" See help for more details but basically what it does is reset the seed of the Rnd function to the system timer which is obviously constantly changing so you get a 'more random' random number!

;-)

If a man says something and there are no women there to hear him, is he still wrong? [ponder]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top