Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Rnd (Random) in VBA

Thanks for the quick replies. Tom, yours did the trick. The only thing
I changed was to adjust
lngNum = Rnd() * 6 + 1
to
lngNum = Rnd() * 5 + 1.

This gives me my random between 1 and 6. The first method gave me up to
7.

This was great. As usual, you are a big help.

Paul



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Rnd (Random) in VBA

I got too cute and forgot that the implicit conversion rounds the number.
Try it this way.

Sub GenRand()
Dim lngNum As Long
Dim i As Long
Range("B2:B200").ClearContents
i = 2
Do
lngNum = Int(Rnd() * 6 + 1)
Cells(i, 2).Value = lngNum
i = i + 1
Loop While lngNum = 6
Next j
End Sub

--
Regards,
Tom Ogilvy


"Paul LaPlant" wrote in message
...
Thanks for the quick replies. Tom, yours did the trick. The only thing
I changed was to adjust
lngNum = Rnd() * 6 + 1
to
lngNum = Rnd() * 5 + 1.

This gives me my random between 1 and 6. The first method gave me up to
7.

This was great. As usual, you are a big help.

Paul



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default Rnd (Random) in VBA

Hello to all

two point
1)
Some times the int() function does not round correctly.
Use Fix()...

2)
To insure you get a different list of numbers each time
use Randomize Timer

Giving:

Public Sub fillRandom
Dim intNumber As Integer
Dim intRow As Integer
'
Range("B2:B200").ClearContents
intRow = 1
Randomize Timer
Do
intNumber = Fix(Rnd() * 6 + 1)
Cells(intRow, 2).Value = intNumber
intRow = intRow + 1
Loop Until intNumber = 6
End Sub


"Tom Ogilvy" wrote in message ...
I got too cute and forgot that the implicit conversion rounds the number.
Try it this way.

Sub GenRand()
Dim lngNum As Long
Dim i As Long
Range("B2:B200").ClearContents
i = 2
Do
lngNum = Int(Rnd() * 6 + 1)
Cells(i, 2).Value = lngNum
i = i + 1
Loop While lngNum = 6
Next j
End Sub

--
Regards,
Tom Ogilvy


"Paul LaPlant" wrote in message
...
Thanks for the quick replies. Tom, yours did the trick. The only thing
I changed was to adjust
lngNum = Rnd() * 6 + 1
to
lngNum = Rnd() * 5 + 1.

This gives me my random between 1 and 6. The first method gave me up to
7.

This was great. As usual, you are a big help.

Paul



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Rnd (Random) in VBA

Int and Fix only differ when dealing with negative numbers. for positive
numbers, the case here, neither round - they both truncate.

While there is nothing wrong with using randomize, I wouldn't see it as
required here. If you don't specify a specific seed, you are not going to
get repeated numbers for sequences less than the period of the generator.

--
Regards,
Tom Ogilvy



"Wouter" wrote in message
om...
Hello to all

two point
1)
Some times the int() function does not round correctly.
Use Fix()...

2)
To insure you get a different list of numbers each time
use Randomize Timer

Giving:

Public Sub fillRandom
Dim intNumber As Integer
Dim intRow As Integer
'
Range("B2:B200").ClearContents
intRow = 1
Randomize Timer
Do
intNumber = Fix(Rnd() * 6 + 1)
Cells(intRow, 2).Value = intNumber
intRow = intRow + 1
Loop Until intNumber = 6
End Sub


"Tom Ogilvy" wrote in message

...
I got too cute and forgot that the implicit conversion rounds the

number.
Try it this way.

Sub GenRand()
Dim lngNum As Long
Dim i As Long
Range("B2:B200").ClearContents
i = 2
Do
lngNum = Int(Rnd() * 6 + 1)
Cells(i, 2).Value = lngNum
i = i + 1
Loop While lngNum = 6
Next j
End Sub

--
Regards,
Tom Ogilvy


"Paul LaPlant" wrote in message
...
Thanks for the quick replies. Tom, yours did the trick. The only

thing
I changed was to adjust
lngNum = Rnd() * 6 + 1
to
lngNum = Rnd() * 5 + 1.

This gives me my random between 1 and 6. The first method gave me up

to
7.

This was great. As usual, you are a big help.

Paul



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



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
Sorting random Data created from a random formula Six Sigma Blackbelt Excel Discussion (Misc queries) 1 September 11th 08 11:03 PM
random ADK Excel Worksheet Functions 1 May 1st 07 06:26 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 07:48 PM.

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"