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

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
The VBA Int function rounds a supplied number down to an integer.

The VBA CInt function converts an expression into an Integer.

PHP:
Public Sub Test()
    MsgBox Int(1.75)
    MsgBox CInt(1.75)
End Sub
 
Upvote 0
What difference would that make

Of course, you could just try it and see for yourself.
VBA Code:
Sub doit()
x = Int(30 * 0.999999999999999)
y = CInt(30 * 0.999999999999999)
MsgBox x & vbNewLine & y
End Sub

30 represents 29-0+1. And 0.999999999999999 represents the largest value that Rnd might return, in theory (close enough; actually, it is 1 - 2^-53).

The result is 29 for Int and 30 for CInt.

The point is: Int truncates, which is what you require. CInt rounds -- and banker's rounding, at that.
 
Upvote 0
I found a 30 instead of 29 max whether I used Int or Cint
Removing the +1 gave the correct 0-29 range.
VBA Code:
Public Sub mytest()
Dim a As Integer
Do
    a = randomvalue(29, 0)
    If a = 0 Or a = 29 Then Debug.Print a;

Loop
End Sub
Public Function randomvalue(top As Integer, low As Integer)
    Randomize    ' Initialize random-number generator.
    'randomvalue = CInt((top - low + 0) * Rnd + low) ' Generate random value between low and top.
    randomvalue = Int(top - low + 0) * Rnd + low ' Generate random value between low and top.
End Function
 
Upvote 0
Both Int and Cint return 1,2, and 3 for : =randomvalue(2,1)
With +1 changes to +0 it's 1 and 2 (for both Int and Cint).
You get something different ?
 
Upvote 0
Is that a command for VBA? I get Sub or Function not defined. Perhapos needs some reference added?
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,718
Members
448,986
Latest member
andreguerra

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