Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 64
Default Return a random number from a list

Hi,

if i have the following list on sheet1
1
2
3
4
5
6
7
8

i want to know how to return any random number from that list on another
sheet.

e.g. sheet2

2
4
5
1
3
6
8
7

and i want to the array to return them in a different order everytime the
code is executed.

can anyone help please?

Many thanks,

Bhupinder.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,101
Default Return a random number from a list

Assuming your numbers are in D1 - D8. Adjust as necessary

=INDEX(D1:D8,RANDBETWEEN(1,COUNTA(D1:D8)),1)

"Bhupinder Rayat" wrote:

Hi,

if i have the following list on sheet1
1
2
3
4
5
6
7
8

i want to know how to return any random number from that list on another
sheet.

e.g. sheet2

2
4
5
1
3
6
8
7

and i want to the array to return them in a different order everytime the
code is executed.

can anyone help please?

Many thanks,

Bhupinder.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 64
Default Return a random number from a list

Thanks Mike,

I filled down, what about if i want each number to appear once only in my
new list, as described below?

Cheers.

"Mike" wrote:

Assuming your numbers are in D1 - D8. Adjust as necessary

=INDEX(D1:D8,RANDBETWEEN(1,COUNTA(D1:D8)),1)

"Bhupinder Rayat" wrote:

Hi,

if i have the following list on sheet1
1
2
3
4
5
6
7
8

i want to know how to return any random number from that list on another
sheet.

e.g. sheet2

2
4
5
1
3
6
8
7

and i want to the array to return them in a different order everytime the
code is executed.

can anyone help please?

Many thanks,

Bhupinder.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,101
Default Return a random number from a list

A bit more difficult but try this

Next to your list of numbers type RAND() and drag down. Sort the selection
by the list of random numbers generated. The numbers 1 to 8 will also sort
with no repeats. Copy this sorted selection to where you want it to be.

"Bhupinder Rayat" wrote:

Thanks Mike,

I filled down, what about if i want each number to appear once only in my
new list, as described below?

Cheers.

"Mike" wrote:

Assuming your numbers are in D1 - D8. Adjust as necessary

=INDEX(D1:D8,RANDBETWEEN(1,COUNTA(D1:D8)),1)

"Bhupinder Rayat" wrote:

Hi,

if i have the following list on sheet1
1
2
3
4
5
6
7
8

i want to know how to return any random number from that list on another
sheet.

e.g. sheet2

2
4
5
1
3
6
8
7

and i want to the array to return them in a different order everytime the
code is executed.

can anyone help please?

Many thanks,

Bhupinder.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Return a random number from a list

Here is some code that will do it:

As written, expects you numbers to start in A1 of sheet1 and go down the
column. Writes the results to sheet2 in the same location. Change the code
to match you data layout and requirements.

Option Explicit


Sub RandomizeRange()
Dim rng As Range
Dim cell As Range
Dim i As Long
With Worksheets("Sheet1")
Set rng = .Range(.Cells(1, 1), _
.Cells(1, 1).End(xlDown))
End With
Randomize
Dim varr() As Variant
ReDim varr(1 To rng.Count)
Dim varr1 As Variant
i = 0
For Each cell In rng
i = i + 1
varr(i) = cell.Value
Next
varr1 = ShuffleArray(varr)
For i = 1 To rng.Count
With Worksheets("sheet2")
.Cells(i, 1).Value = varr1(i)
End With
Next
End Sub

Public Function ShuffleArray(varr)

'
' Algorithm from:
' The Art of Computer Programming: _
' SemiNumerical Algorithms Vol 2, 2nd Ed.
' Donald Knuth
' p. 139
'
'
Dim List() As Long
Dim t As Long
Dim i As Long
Dim j As Long
Dim k As Long
Dim lngTemp As Long

t = UBound(varr, 1) - LBound(varr, 1) + 1
ReDim List(1 To t)
For i = 1 To t
List(i) = varr(i)
Next
j = t
Randomize
For i = 1 To t
k = Rnd() * j + 1
lngTemp = List(j)
List(j) = List(k)
List(k) = lngTemp
j = j - 1
Next
ShuffleArray = List
End Function



--
Regards,
Tom Ogilvy


"Bhupinder Rayat" wrote:

Thanks Mike,

I filled down, what about if i want each number to appear once only in my
new list, as described below?

Cheers.

"Mike" wrote:

Assuming your numbers are in D1 - D8. Adjust as necessary

=INDEX(D1:D8,RANDBETWEEN(1,COUNTA(D1:D8)),1)

"Bhupinder Rayat" wrote:

Hi,

if i have the following list on sheet1
1
2
3
4
5
6
7
8

i want to know how to return any random number from that list on another
sheet.

e.g. sheet2

2
4
5
1
3
6
8
7

and i want to the array to return them in a different order everytime the
code is executed.

can anyone help please?

Many thanks,

Bhupinder.

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
Random number from a list leerem Excel Discussion (Misc queries) 1 September 25th 08 01:55 PM
generating random number list yvette Excel Worksheet Functions 1 January 12th 07 05:07 PM
Generate random number from a list Arnie Excel Worksheet Functions 6 November 7th 06 07:55 AM
next logical number from list of about 2000 random numbers gerryd Excel Programming 2 September 19th 06 05:00 PM
How do I find random number in list of random alpha? (Position is. jlahealth-partners Excel Discussion (Misc queries) 0 February 8th 05 05:31 PM


All times are GMT +1. The time now is 06:53 AM.

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

About Us

"It's about Microsoft Excel"