PC Review


Reply
Thread Tools Rate Thread

How to create a random number between a range?

 
 
Dave
Guest
Posts: n/a
 
      3rd Feb 2008
I need to generate random whole numbers between the range of 21 and 90.

I started with this:
=rand()*90
with the cells formatted to zero decimal places.

how do I limit the lower number?

Any help here will be appreciated.

Thanks in advance

dave

 
Reply With Quote
 
 
 
 
Tyro
Guest
Posts: n/a
 
      3rd Feb 2008
=RAND()*(b-a)+a taken from the Excel help file. So in your case
RAND()*(90-21)+21. If you want integers only then =INT(RAND()*(90-21)+21
In Excel 2007, = RANDBETWEEN(21,90)

Tyro

"Dave" <dave@accessdatapros> wrote in message
news:56FD1420-58DC-4385-A784-(E-Mail Removed)...
>I need to generate random whole numbers between the range of 21 and 90.
>
> I started with this:
> =rand()*90
> with the cells formatted to zero decimal places.
>
> how do I limit the lower number?
>
> Any help here will be appreciated.
>
> Thanks in advance
> dave
>



 
Reply With Quote
 
Tyro
Guest
Posts: n/a
 
      3rd Feb 2008
Correction: (as usual)
If you want integers only then =INT(RAND()*(90-21))+21

Tyro

"Tyro" <(E-Mail Removed)> wrote in message
news:fsspj.10541$(E-Mail Removed)...
> =RAND()*(b-a)+a taken from the Excel help file. So in your case
> RAND()*(90-21)+21. If you want integers only then =INT(RAND()*(90-21)+21
> In Excel 2007, = RANDBETWEEN(21,90)
>
> Tyro
>
> "Dave" <dave@accessdatapros> wrote in message
> news:56FD1420-58DC-4385-A784-(E-Mail Removed)...
>>I need to generate random whole numbers between the range of 21 and 90.
>>
>> I started with this:
>> =rand()*90
>> with the cells formatted to zero decimal places.
>>
>> how do I limit the lower number?
>>
>> Any help here will be appreciated.
>>
>> Thanks in advance
>> dave
>>

>
>



 
Reply With Quote
 
Gord Dibben
Guest
Posts: n/a
 
      4th Feb 2008
The ATP function randbetween could be used

=randbetween(21,90) drag down 70 rows.

Unfortunately this can and will create duplicates.

See John McGimpsey's site for how to prevent duplicates.

http://www.mcgimpsey.com/excel/udfs/randint.html

Using RAND() in a column adjacent to a column with numbers 21 through 90 works
quite well.


Gord Dibben MS Excel MVP

On Sun, 3 Feb 2008 15:38:35 -0800, "Dave" <dave@accessdatapros> wrote:

>I need to generate random whole numbers between the range of 21 and 90.
>
>I started with this:
>=rand()*90
>with the cells formatted to zero decimal places.
>
>how do I limit the lower number?
>
>Any help here will be appreciated.
>
>Thanks in advance
>
>dave
>


 
Reply With Quote
 
Dave
Guest
Posts: n/a
 
      4th Feb 2008
Thanks for the replies.
I am in 2003 so I can use RANDBETWEEN(21,90) function.
However Tyro's solution works great and dups are not an issue in this case.

Thanks much

dave


"Dave" <dave@accessdatapros> wrote in message
news:56FD1420-58DC-4385-A784-(E-Mail Removed)...
> I need to generate random whole numbers between the range of 21 and 90.
>
> I started with this:
> =rand()*90
> with the cells formatted to zero decimal places.
>
> how do I limit the lower number?
>
> Any help here will be appreciated.
>
> Thanks in advance
> dave
>

 
Reply With Quote
 
Tyro
Guest
Posts: n/a
 
      4th Feb 2008
The solution of =INT(RAND()*(90-21)+21, taken from the Excel help file from
RAND()*(b-a)+a is wrong because RAND()*(b-a)+a never produces b. The reason
for that is the RAND function never produces 1 as a result. The RAND
function is usually described in most documentation as producing a value
between 0 and 1 actually produces a value between 0 and .999999999999999 as
the help file says. Therefore, it can never produce b. If I want a value
between 0 and 2, the formula RAND()*(2-0)+0 will never produce 2. The
maximum it can produce is 2 * .9999999999999999 = 1.99999999999999. So, for
you to get random values from 21through 90, inclusive, the formula needs to
be: =INT(RAND(91-21))+21. I didn't realize the Excel help was wrong until I
thought of the .9999999999 limitation on the return value from RAND().

Tyro


"Dave" <dave@accessdatapros> wrote in message
news:56FD14sss20-58DC-4385-A784-(E-Mail Removed)...
>I need to generate random whole numbers between the range of 21 and 90.
>
> I started with this:
> =rand()*90
> with the cells formatted to zero decimal places.
>
> how do I limit the lower number?
>
> Any help here will be appreciated.
>
> Thanks in advance
> dave
>



 
Reply With Quote
 
Stan Brown
Guest
Posts: n/a
 
      4th Feb 2008
Sun, 3 Feb 2008 15:38:35 -0800 from Dave <dave@accessdatapros>:
> I need to generate random whole numbers between the range of 21 and 90.
>
> I started with this:
> =rand()*90
> with the cells formatted to zero decimal places.


=RANDBETWEEN(21,90) is by far the easiest way. (Make sure you have
Analysis Toolpak installed.)

If you want a whole number 21 to 90, and you limit yourself to rand
(), then you need =21+int(79.99999999*rand()) or something along
those lines.

--
Stan Brown, Oak Road Systems, Tompkins County, New York, USA
http://OakRoadSystems.com/
"If there's one thing I know, it's men. I ought to: it's
been my life work." -- Marie Dressler, in /Dinner at Eight/
 
Reply With Quote
 
Stan Brown
Guest
Posts: n/a
 
      4th Feb 2008
Sun, 3 Feb 2008 15:51:38 -0800 from Tyro <(E-Mail Removed)>:
> =RAND()*(b-a)+a taken from the Excel help file. So in your case
> RAND()*(90-21)+21. If you want integers only then =INT(RAND()*(90-21)+21


I don't think this is correct, even leaving aside the matter of your
missing parenthesis. rand() returns a random real number
0 <= rand() < 1,
so
0 <= rand()*79 < 79
But int() truncates, so the whole numbers will be between 0 and 78,
not 0 and 79.

> In Excel 2007, = RANDBETWEEN(21,90)


It's not limited to Excel 2007; works just fine in Excel 2003.

--
Stan Brown, Oak Road Systems, Tompkins County, New York, USA
http://OakRoadSystems.com/
"If there's one thing I know, it's men. I ought to: it's
been my life work." -- Marie Dressler, in /Dinner at Eight/
 
Reply With Quote
 
Stan Brown
Guest
Posts: n/a
 
      4th Feb 2008
Sun, 3 Feb 2008 15:53:22 -0800 from Tyro <(E-Mail Removed)>:
> Correction: (as usual)
> If you want integers only then =INT(RAND()*(90-21))+21


That will give integers but from 21 to 89 not 21 to 90.

--
Stan Brown, Oak Road Systems, Tompkins County, New York, USA
http://OakRoadSystems.com/
"If there's one thing I know, it's men. I ought to: it's
been my life work." -- Marie Dressler, in /Dinner at Eight/
 
Reply With Quote
 
Stan Brown
Guest
Posts: n/a
 
      4th Feb 2008
Sorry, 90-21 is 69 not 79! Corrected version:

Sun, 3 Feb 2008 15:51:38 -0800 from Tyro <(E-Mail Removed)>:
> =RAND()*(b-a)+a taken from the Excel help file. So in your case
> RAND()*(90-21)+21. If you want integers only then =INT(RAND()*(90-21)+21


I don't think this is correct, even leaving aside the matter of your
missing parenthesis. rand() returns a random real number
0 <= rand() < 1,
so
0 <= rand()*(90-21) < 69
But int() truncates, so the whole numbers will be between 0 and 68,
not 0 and 69. And the given formula
=INT(RAND()*(90-21)+21
will give values 21 to 89, not 21 to 90.

> In Excel 2007, = RANDBETWEEN(21,90)


It's not limited to Excel 2007; works just fine in Excel 2003.


--
Stan Brown, Oak Road Systems, Tompkins County, New York, USA
http://OakRoadSystems.com/
"If there's one thing I know, it's men. I ought to: it's
been my life work." -- Marie Dressler, in /Dinner at Eight/
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Random Number between a Range of Numbers Philosophaie Microsoft Excel Programming 1 5th Oct 2009 08:59 AM
Random number from range =?Utf-8?B?QW50aG9ueQ==?= Microsoft Excel Programming 6 4th Mar 2007 05:25 PM
Random Number list without duplicates in a range. =?Utf-8?B?QW5kcmV3?= Microsoft Access Forms 1 21st Sep 2006 12:10 AM
Multiple random numbers from the same number range barnettcb Microsoft Access 2 15th Sep 2006 11:10 PM
Generating Random Number In A Range Barney Microsoft Windows 2000 CMD Promt 3 29th Apr 2004 10:49 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:34 AM.