Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default exclude a number from randbetween function

I am trying to get 4 different random numbers, all between 1 and 20, without
duplicating the numbers. Can anyone advise me on how to do this. I am
curently using randbetween function, 4 times, but I get the same number twice
every now and again
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default exclude a number from randbetween function

Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware

'Generates five random numbers between 1 and 20
'with no duplicates. Concept stolen from Tom Ogilvy.
'Jim Cone - San Francisco, USA
Sub GetThem()
Dim arrCheck(1 To 20) As Long
Dim arrList(1 To 5) As Long
Dim j As Long
Dim N As Long
Const LNG_PLUG As Long = 999

j = 1
Do While j < 6
'Get a random number
Randomize
N = Int(Rnd * 20 + 1)
'If number unique then add to arrList.
If arrCheck(N) < LNG_PLUG Then
arrList(j) = N
arrCheck(N) = LNG_PLUG
j = j + 1
End If
Loop

Range("B5:F5").Value = arrList()
End Sub
'-----------------



"Koreknots"

wrote in message
I am trying to get 4 different random numbers, all between 1 and 20, without
duplicating the numbers. Can anyone advise me on how to do this. I am
curently using randbetween function, 4 times, but I get the same number twice
every now and again
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default exclude a number from randbetween function


A formula method...

In A1 =RAND() copied down to A20

in B1 copied down to B4

=RANK(A1,A$1:A$20)

These are your 4 random numbers - hit F9 to regenerate - hide column
if require

--
daddylongleg
-----------------------------------------------------------------------
daddylonglegs's Profile: http://www.excelforum.com/member.php...fo&userid=3048
View this thread: http://www.excelforum.com/showthread.php?threadid=56447

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default exclude a number from randbetween function

Thanks, works a treat.

"daddylonglegs" wrote:


A formula method...

In A1 =RAND() copied down to A20

in B1 copied down to B4

=RANK(A1,A$1:A$20)

These are your 4 random numbers - hit F9 to regenerate - hide column A
if required


--
daddylonglegs
------------------------------------------------------------------------
daddylonglegs's Profile: http://www.excelforum.com/member.php...o&userid=30486
View this thread: http://www.excelforum.com/showthread...hreadid=564478


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to keep random number from changing using RANDBETWEEN? TXlimogirl Excel Worksheet Functions 6 April 3rd 23 04:41 PM
Randbetween function mike_vr Excel Discussion (Misc queries) 12 March 10th 10 01:21 AM
randbetween function Tracey Excel Discussion (Misc queries) 5 February 11th 06 12:23 AM
Why doesn't my =RANDBETWEEN function work? DanielWalters6 Excel Discussion (Misc queries) 4 December 19th 05 10:28 AM
randbetween function No Name Excel Programming 1 July 7th 04 09:25 PM


All times are GMT +1. The time now is 09:50 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"