#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default random?

Hi,
A friend asked me this question regarding random numbers in VBA
If you run the following code:

Sub rand()
A = 1
For i = 1 To 10
Cells(A, 3) = rnd
A = A + 1
Next i
End Sub

10 random numbers will appear in A1:A10
Now hit the stop (reset) button in the VBA window and run again.
The exact same 'random' numbers appear.

Is this expected?
Is there a work-around?

Regards - Dave.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default random?

you need to use the keyword, randomize .

Sub rand()
A = 1
For i = 1 To 10
Randomize
Cells(A, 3) = Rnd
A = A + 1
Next i
End Sub

--


Gary

"Dave B" wrote in message
...
Hi,
A friend asked me this question regarding random numbers in VBA
If you run the following code:

Sub rand()
A = 1
For i = 1 To 10
Cells(A, 3) = rnd
A = A + 1
Next i
End Sub

10 random numbers will appear in A1:A10
Now hit the stop (reset) button in the VBA window and run again.
The exact same 'random' numbers appear.

Is this expected?
Is there a work-around?

Regards - Dave.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,549
Default random?

Even better - place the randomize statement outside of the loop.
--
Jim Cone
Portland, Oregon USA



"Gary Keramidas"
wrote in message
you need to use the keyword, randomize .

Sub rand()
A = 1
For i = 1 To 10
Randomize
Cells(A, 3) = Rnd
A = A + 1
Next i
End Sub
--
Gary

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,388
Default random?

Thanks guys.
I guess that randomizes the 'random' numbers.
Regards - Dave.
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 947
Default random?

As a side note, the Worksheet version doesn't need to be Randomized in Excel 2007. Not as efficient though if you have to do this often.
Note that since 'A' & 'i' are equal in your loop, you can eliminate one of the variables.

Sub Demo()
Dim R As Long '(R)ow
For R = 1 To 10
Cells(R, 2) = [Rand()]
Next R
End Sub



A = 1
For i = 1 To 10
A = A + 1
Next i


--
Dana DeLouis


"Dave" wrote in message ...

Thanks guys.
I guess that randomizes the 'random' numbers.
Regards - Dave.


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 54
Default random?

On Tue, 30 Sep 2008 05:51:01 -0700, Dave
wrote:

Thanks guys.
I guess that randomizes the 'random' numbers.
Regards - Dave.


It randomizes the initial seed for the random number generator.
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Random phantom breakpoints and random bogus compile errors Greg Lovern Excel Programming 5 June 2nd 10 03:52 PM
Sorting random Data created from a random formula Six Sigma Blackbelt Excel Discussion (Misc queries) 1 September 11th 08 11:03 PM
Non-random numbers generated by excel's data analysis random gener Allie Excel Worksheet Functions 10 September 17th 05 06:19 AM
Non-random numbers generated by excel's data analysis random gener Harlan Grove Excel Discussion (Misc queries) 2 September 13th 05 04:06 PM
How do I find random number in list of random alpha? (Position is. jlahealth-partners Excel Discussion (Misc queries) 0 February 8th 05 05:31 PM


All times are GMT +1. The time now is 02:28 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"