Random Number

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
1,835
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
I found this via Google but just wonder if it's correct
VBA Code:
Public Function randomvalue(top As Integer, low As Integer)
    Randomize    ' Initialize random-number generator.
    randomvalue = CInt((top - low + 1) * Rnd + low) ' Generate random value between low and top.
End Function
Because if I send in a Top of 29 and a low of 0 I have found it will return 30
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Why not use: = RANDBETWEEN (0.29)?
Is that a command for VBA? I get Sub or Function not defined. Perhapos needs some reference added?

I think Phuoc assumes that you call your randomvalue function from Excel. Of course, your test demonstrates that you might not.

In VBA, you could write:

randomvalue = WorksheetFunction.RandBetween(low, top)

But if randomvalue is called from Excel (or from a VBA function that is called from Excel), the major difference is: RANDBETWEEN and WorksheetFunction.RandBetween cause the Excel formula to be "volatile". That is, the Excel formula -- and any cell that references it directly or indirectly -- is recalculated whenever any cell in the entire workbook is edited, and at other times that Excel decides to recalculate any part of the workbook.

Your use of Rnd makes your randvalue function non-volatile.
 
Upvote 0
I'm doing my tests in Excel, but I'm not using a worksheet. Just VBA module.
Debug.Print WorksheetFunction.RandBetween(0, 29) still brings up an error
But = Int((top-low+1)*Rnd) + low is testing good. I'm getting both 0 and 29 and no <0 or > 30 so I'll go with that. Good to have it right.
Thanks.
 
Upvote 0
My apologies... although it didn't work the first time (and I only tried once) it did now..
Both acquired the dropdown list... (using Excel 2010)
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,380
Members
448,955
Latest member
BatCoder

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