generate a random number to enter in a cell

brianfosterblack

Active Member
Joined
Nov 1, 2011
Messages
251
I am am looping through a range on my computer (Excel 2013) and where I find a number 1 I want to replace it with a random number between the bottom number in Cell I9 and the top number in cell I11
So what I need my code to do is Activecell.value= "Randbetween Range ("I9").value and Range ("I11").Value
Can someone help with the code to do this?
The number generated must be a whole number
 
Last edited:

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Code:
Function RandNum()
    Randomize
    RandNum = Int((Range("I11") - Range("I9") + 1) * Rnd + Range("I9"))
End Function
 
Upvote 0
Simply:

ActiveCell = WorksheetFunction.RandBetween(Range("I9"), Range("I11"))

Although, if you are "looping through a range", there is no need to continuously change ActiveCell.

Code:
Dim r As Range
For each r in myRange
    If r = 1 Then r = WorksheetFunction.RandBetween(Range("I9"), Range("I11"))
Next

It is better to use RandBetween than VBA Rnd. The Excel RNG is better than VBA Rnd, since Excel 2010 (2007?). I assume the faster execution of VBA Rnd is not a factor.

PS.... For efficiency, it is better to minimize access to the Excel cells. So the following is better:

Dim lo As Long, hi As Long
lo = Range("I9")
hi = Range("I11")
[....] WorksheetFunction.RandBetween(lo, hi)

Change Long to Double if lo and hi might exceed the integer limit of type Long (!).

Also, that does assume that I9 and I11 contain integers (or you want them rounded to integers). After all, you indicated that you wanted to use RANDBETWEEN, in the first place.
 
Last edited:
Upvote 0
It is better to use RandBetween than VBA Rnd. The Excel RNG is better than VBA Rnd, since Excel 2010 (2007?).

Please explain the difference. As far as I know, random means random!
 
Last edited:
Upvote 0
Lots.

The VBA Rnd() function is about 1000 times faster in VBA than WorksheetFunction.Rand.

Rnd() has a very short cycle (2^24). In Excel 2007, RAND() used a linear congruential generator with a period of ~ 2^44. In Excel 2010, RAND was changed to use a Mersenne Twister algorithm with a ridiculously long period (~2^19937)

Rnd() can be initialized to restart with any number in the pattern, which is convenient for test. RAND() cannot.
 
Upvote 0
I knew VBA Rnd() was superior to Rand, and so thought RandBetween would be in the same boat as Rand as it is a worksheet function. That is why I asked the difference between RandBetween and Rnd()
 
Last edited:
Upvote 0
It's superior only in speed and the ability to initialize it.

It's useless for generating unpredictable random sequences (e.g., dice rolls), because after observing a few rolls (maybe 20), it's possible to predict all the rest unless it is re-randomized.

EDIT: I don't know how RANDBETWEEN() works; I assume it uses the MT RAND() function, but don't know that for a fact.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,414
Messages
6,119,373
Members
448,888
Latest member
Arle8907

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