Welcome to MSDN Blogs Sign in | Join | Help

News

Excel Can Be 'Rand'om Too

A few weeks ago I showed you a neat trick that you can use in Word to quickly fill up a document with sample text.

Not to be outdone by its sibling, Excel also features a few ways to swiftly fill up a worksheet with sample data.  We use these functions extensively in order to help test Excel, but people have reported finding them useful from time to time for their own uses.

The simplest way to fill a cell with a random value is to select the cell and type:

    =rand()

Voila, the cell is populated with a random number between 0 and 1.  From now on, every time Excel recalculates the sheet, a different number between 0 and 1 will appear in this cell.

To fill an entire range of cells at once with random values, start by dragging out the rectangle of cells you want to fill.  Now, type:

    =rand()

And then press CTRL+ENTER.  Excel fills all of the selected cells with different random numbers between 0 and 1.

Now, let's get a little fancier.  Excel also includes a function RANDBETWEEN which allows you to specify a lower bound and an upper bound for the random integer Excel puts in a cell.

Try this: select a range of cells.  Then type:

    =randbetween(50, 700)

And then press CTRL+ENTER.  Excel fills all of the selected cells with different random numbers between 50 and 700.  As with RAND, the numbers change whenever Excel does recalc (you can force this by pressing F9.)

Note: If you try RANDBETWEEN but get an #NAME error instead of the random numbers, you don't have the Analysis ToolPak running.  Click Tools.Add-Ins and check the box next to "Analysis ToolPak" to enable it.  Then press F2 to cause Excel to regenerate all formulas.

Finally, if you want the random values to stay as they are (and not change every time Excel does recalc), you need to convert the functions to values.  One easy way to do this:

  • Select the range of cells you want to fill up
  • Click Edit.Copy to copy the cells to the clipboard
  • Click Edit.Paste Special
  • Select "Values" in the dialog box, click OK.

Ta-da, the random numbers are now locked in place and won't change anymore.  (By the way, doing this same operation in Excel 12 takes 3 clicks instead of 6!)

So, as you can see, Excel has 'rand'om functionality of its own which you can use to quickly fill up a spreadsheet with random numbers.

Warning: Not recommended for SEC disclosures or during financial audits.

Posted: Friday, October 14, 2005 7:00 AM by jensenh
Filed under:

Comments

Andy Cotgreave said:

The best bit in this post is your little revelation that Paste Special is only 3 clicks in Office 12.

That's great news!
# October 14, 2005 10:08 AM

Mario Goebbels said:

Guessing from Excel screenshots I've seen, you click the Copy button on the clipboard ribbon, then on the arrow under the huge paste button to finally click an soon-to-be-listed option called Paste Values. Am I right?
# October 14, 2005 10:17 AM

jensenh said:

On the 3 clicks: Copy, Paste (which is a split button), Values.

Mario wins the prize.
# October 14, 2005 1:06 PM

Jon Peltier said:

(By the way, doing this same operation in Excel 12 takes 3 clicks instead of 6!)

No, it's two clicks on Excel 9-10-11. One on copy (or Ctrl+C), and one click on the custom Paste Special--Values button on my custom toolbar. I also have Paste Special--Formulas, Paste Special--Formats, and Paste Special--Values/Transposed. This is the kind of flexibility we're concerned about retaining as we move forward.

(Don't get me wrong, I'm a big fan already.)
# October 14, 2005 8:39 PM

TC said:

> Excel fills all of the selected cells with
> different random numbers between 0 and 1.

If they're guaranteed different, they ain't random! :-)
# October 15, 2005 5:09 AM

Harvey Motulsky said:

In a previous post, you explained how Office 12 strived to avoid hiding commands 'under rocks'. Clicking Control-Enter to enter an array formula is way more obscure than that. It comes out of thin air, with no way to discover (other than reading books). Will array formulae be improved in Excel 12?
# October 15, 2005 8:47 AM

PatriotB said:

It could be 4 clicks -- if you are on a different ribbon than the one that contains the Clipboard chunk, you'd have to click the appopriate tab to get to that.
# October 15, 2005 1:34 PM

Mario Goebbels said:

The clipboard ribbon is probably one of the ever present ones.
# October 16, 2005 7:35 AM

Kawigi said:

Clipboard is a chunk on the Sheet tab of the ribbon. It's ever present, but not always in front (so PatriotB makes a good point).
# October 17, 2005 1:33 PM
New Comments to this post are disabled
Page view tracker