Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,388
Default Fill with random numbers

Hi,

The range A1 - H200 is 1600 cells and I need to fill those cells randomely
with the numbers 1 to 1600, no number must repeat. I played with simply
putting those numbers in the cells and used RAND() in a helper column but
this doesn't really give the randomness I require. Any help please.

D
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Fill with random numbers

Hi Dave: Try this. It uses columns I & J as "helper" columns:

Sub luxation()
For i = 1 To 1600
Cells(i, "I").Value = i
Cells(i, "J") = Evaluate("=rand()")
Next
Columns("I:J").Sort Key1:=Range("J1"), Order1:=xlAscending, Header:=xlNo
k = 1
For i = 1 To 200
For j = 1 To 8
Cells(i, j).Value = Cells(k, "I").Value
k = k + 1
Next
Next
Columns("I:J").Clear
End Sub

--
Gary''s Student - gsnu200838


"Dave" wrote:

Hi,

The range A1 - H200 is 1600 cells and I need to fill those cells randomely
with the numbers 1 to 1600, no number must repeat. I played with simply
putting those numbers in the cells and used RAND() in a helper column but
this doesn't really give the randomness I require. Any help please.

D

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Fill with random numbers

You may want to visit J.E. McGimpsey's site and grab a copy of his =RandInt()
function.
http://www.mcgimpsey.com/excel/udfs/randint.html

If you're new to macros:

Debra Dalgleish has some notes how to implement macros he
http://www.contextures.com/xlvba01.html

David McRitchie has an intro to macros:
http://www.mvps.org/dmcritchie/excel/getstarted.htm

Ron de Bruin's intro to macros:
http://www.rondebruin.nl/code.htm

(General, Regular and Standard modules all describe the same thing.)

Dave wrote:

Hi,

The range A1 - H200 is 1600 cells and I need to fill those cells randomely
with the numbers 1 to 1600, no number must repeat. I played with simply
putting those numbers in the cells and used RAND() in a helper column but
this doesn't really give the randomness I require. Any help please.

D


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 806
Default Fill with random numbers

Hello,

Another one which offers unique numbers as well as (optional!)
repeating numbers (up to a given limit) and optimizes for a small
count of draws in a huge range:
http://www.sulprobil.com/html/uniqrandint.html

Regards,
Bernd
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Fill with random numbers


Get the code for UniqueRandomLongs from
www.cpearson.com/Excel/RandomNumbers.aspx. This will create a
1-dimensional array with no-repeating random numbers. Then, use that
code in

Sub AAA()
Dim Arr As Variant
Dim Arr2(1 To 200, 1 To 8) As Long
Dim R As Long
Dim C As Long
Dim N As Long
N = 0
Arr = UniqueRandomLongs(1, 1600, 1600)
For R = 1 To 200
For C = 1 To 8
N = N + 1
Arr2(R, C) = Arr(N)
Next C
Next R
Range("A1:H200").Value = Arr2
End Sub

This code converts the 1D array from UniqueRandomLongs to a 2D array
and then puts that array into A1:H200.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)



On Wed, 11 Mar 2009 12:09:25 -0700, Dave
wrote:

Hi,

The range A1 - H200 is 1600 cells and I need to fill those cells randomely
with the numbers 1 to 1600, no number must repeat. I played with simply
putting those numbers in the cells and used RAND() in a helper column but
this doesn't really give the randomness I require. Any help please.

D



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,388
Default Fill with random numbers

Gents,

Apologies for the single reply to all of your excellent solutions all of
which worked flawlessly. I am most grateful.

D

"Chip Pearson" wrote:


Get the code for UniqueRandomLongs from
www.cpearson.com/Excel/RandomNumbers.aspx. This will create a
1-dimensional array with no-repeating random numbers. Then, use that
code in

Sub AAA()
Dim Arr As Variant
Dim Arr2(1 To 200, 1 To 8) As Long
Dim R As Long
Dim C As Long
Dim N As Long
N = 0
Arr = UniqueRandomLongs(1, 1600, 1600)
For R = 1 To 200
For C = 1 To 8
N = N + 1
Arr2(R, C) = Arr(N)
Next C
Next R
Range("A1:H200").Value = Arr2
End Sub

This code converts the 1D array from UniqueRandomLongs to a 2D array
and then puts that array into A1:H200.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)



On Wed, 11 Mar 2009 12:09:25 -0700, Dave
wrote:

Hi,

The range A1 - H200 is 1600 cells and I need to fill those cells randomely
with the numbers 1 to 1600, no number must repeat. I played with simply
putting those numbers in the cells and used RAND() in a helper column but
this doesn't really give the randomness I require. Any help please.

D


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default Fill with random numbers

Hi,

I assume your tried manually entering the numbers and then sorted using a
helper column and RAND() and this is wasn't what suited your needs. I can't
visualise a worksheet formula solution to this so maybe this VB one. Right
click your sheet tab, view code and paste this in and run it. You could maybe
assign a button to run it or run it from tools|Macros so you can see the
worksheet populating with data and how it begins to struggle finding the
unique numbers for the last dozen or so cells.

Sub Liminal_Advertising()
Dim FillRange As Range
Set FillRange = Range("A1:H200")
For Each c In FillRange
Do
c.Value = Int((1600 * Rnd) + 1)
Loop Until WorksheetFunction.CountIf(FillRange, c.Value) < 2
Next
End Sub

Mike

"Dave" wrote:

Hi,

The range A1 - H200 is 1600 cells and I need to fill those cells randomely
with the numbers 1 to 1600, no number must repeat. I played with simply
putting those numbers in the cells and used RAND() in a helper column but
this doesn't really give the randomness I require. Any help please.

D

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Fill with random numbers

Here's one more...

Sub FillRangeWithRandomNumbers()
Dim R As Range
Dim X As Long
Dim Temp As Long
Dim RandomIndex As Long
Dim RandomNumbers() As Long
Const CellRange As String = "A1:H200"
ReDim RandomNumbers(1 To Range(CellRange).Count)
' Initialize the RandomNumbers array
For X = 1 To UBound(RandomNumbers)
RandomNumbers(X) = X
Next
' Randomize the RandomNumbers array
For X = UBound(RandomNumbers) To 1 Step -1
RandomIndex = Int((X - LBound(RandomNumbers) + 1) * _
Rnd + LBound(RandomNumbers))
Temp = RandomNumbers(RandomIndex)
RandomNumbers(RandomIndex) = RandomNumbers(X)
RandomNumbers(X) = Temp
Next
' Distribute the random numbers to the Range
X = 1
For Each R In Range(CellRange)
R.Value = RandomNumbers(X)
X = X + 1
Next
End Sub

--
Rick (MVP - Excel)


"Dave" wrote in message
...
Hi,

The range A1 - H200 is 1600 cells and I need to fill those cells randomely
with the numbers 1 to 1600, no number must repeat. I played with simply
putting those numbers in the cells and used RAND() in a helper column but
this doesn't really give the randomness I require. Any help please.

D


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
getting numbers divisible by 4 from random numbers in ascending order ramana Excel Worksheet Functions 6 June 19th 07 06:41 PM
Can Excel pick random numbers from 1-300 and not repeat numbers? Julian Excel Discussion (Misc queries) 1 June 7th 06 07:17 AM
Non-random numbers generated by excel's data analysis random gener Allie Excel Worksheet Functions 10 September 17th 05 06:19 AM
Non-random numbers generated by excel's data analysis random gener Harlan Grove Excel Discussion (Misc queries) 2 September 13th 05 04:06 PM
fill random cells hulub Excel Programming 1 September 19th 04 08:26 PM


All times are GMT +1. The time now is 02:39 AM.

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"