random number in macro

captsalmon

New Member
Joined
Oct 29, 2012
Messages
6
I hope is is a quick and easy question. I tried to use rnd to generate a random number in a macro, but it just returns 0.522.... repeatedly.
I tried any number of variations on this, including rnd(), and even applications.worksheetfunction.rand(), but nothing.

so what am I doing wrong?

thanks in advance!
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Can you post your code to show how you using the rnd function?
 
Upvote 0
The Rnd function returns a value less than 1 but greater than or equal to zero.

The value of Number determines how Rnd generates a pseudo-random number:

  • 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.
To produce random integers in a given range, use this formula:
Int ((upperbound - lowerbound + 1) * Rnd + lowerbound)


If you want to get an integer value you can use the following, for example between 1 and 10.
VBA Code:
Sub test()
  Dim n As Long
 
  'for example between 1 to 10:
'sintax:
'Int ((upperbound - lowerbound + 1) * Rnd + lowerbound)
 
  Randomize
  n = Int((10 - 1 + 1) * Rnd + 1)
  MsgBox n
End Sub
 
Upvote 0
Can you post your code to show how you using the rnd function?
I figured it out. it is returning a random number....but from the same seed variable, so the first one is always the same. this I did not know. I was just testing what I thought was simple code, and say 0.533 every time...so thought that it was not returning a random #. still seems crazy that everytime it will return the same series of 'random number'.

but thanks for your response...always good to know folks are willing to help out.
 
Upvote 0
The Rnd function returns a value less than 1 but greater than or equal to zero.

The value of Number determines how Rnd generates a pseudo-random number:

  • 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.
To produce random integers in a given range, use this formula:
Int ((upperbound - lowerbound + 1) * Rnd + lowerbound)


If you want to get an integer value you can use the following, for example between 1 and 10.
VBA Code:
Sub test()
  Dim n As Long
 
  'for example between 1 to 10:
'sintax:
'Int ((upperbound - lowerbound + 1) * Rnd + lowerbound)
 
  Randomize
  n = Int((10 - 1 + 1) * Rnd + 1)
  MsgBox n
End Sub
thanks for the info. I figured out my original problem, and it was that I was just looking at a single output (the first run of a loop), and I was getting the same value, so thought it was not creating a random number. turns out it is...but from the same seed varible, and so always the same series of values. not very 'random' in my book. anyway I can introduce a new seed value with each run of the macro?
 
Upvote 0
anyway I can introduce a new seed value with each run of the macro?
Sure, in the variable n you will have a new random number in each execution:

VBA Code:
Sub NewRandomNumber()
  Dim n As Double
  Randomize
  n = Rnd
  MsgBox n
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,497
Members
448,967
Latest member
visheshkotha

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