Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default Variable count and bound random numbers

I need to create a variable sized list of random numbers with a
variable sized range without duplicate values. I have been creating
the list with a variable range with the following code then manually
changing any dup values (checking changes by sorting the list and
filtering uniques). I would like to replace this with something that
uses the count, default and input box to create the list without dups
on the first try. I tried looking through the site for a solution but
the stuff that looked like it might be what I wanted I couldn't figure
out how to add my variables to at all. Thank you in advance for any
assistance.

Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Dim reccount As Double
Dim recdefault As Double
Dim trunrecdef As Integer
Dim recpct As Double

reccount = Selection.Count
'counts records to determine upperbound
recdefault = 117
'default number of random numbers required
trunrecdef = Fix(recdefault)
'this step gives whole # if math on default
Do
recpct = Application.InputBox(Prompt:=("How many PRV lots to
keep" & vbLf & "Max lots allowed " &_ reccount),Title:="Number PRV
lots to test", Default:=trunrecdef, Type:=1)
Loop Until recpct <= reccount
'input number of random numbers, wont take #
higher than reccount
ActiveWindow.Close
'closes file count was performed on
Workbooks.Open Filename:= _
"T:\Groups\PAM\Meat Juice Lab\Processing lab B7\Current
selective\s_5 to 2 PSS\N_RandID.xls"
'file rand # list goes on
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
Range("A2").Select
ActiveCell.FormulaR1C1 = "=RANDBETWEEN(1," & reccount & ")"
' sets randbetween to 1-count of records
Selection.Copy
ActiveCell.Offset(1, 0).Select
StartCell = ActiveCell.Offset(0, 0).Address
EndCell = ActiveCell.Offset(recpct - 2, 0).Address
Range(StartCell, EndCell).Select
'selects range to copy randbetween to
ActiveSheet.Paste
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Variable count and bound random numbers

I used the VBA random function a put values into the cells rather than a
formula. The find function makes sure there are no duplicates.

For Count = 1 To Recpct
Set SearchRange = Range("A2:A" & (Count + 1))
Do
RandNum = ((reccount - 1) * Rnd()) + 1
Set c = SearchRange.Find(what:=RandNum, LookIn:=xlValues)
Loop While Not c Is Nothing
Range("A" & (Count + 1)) = RandNum
Next Count


"goaljohnbill" wrote:

I need to create a variable sized list of random numbers with a
variable sized range without duplicate values. I have been creating
the list with a variable range with the following code then manually
changing any dup values (checking changes by sorting the list and
filtering uniques). I would like to replace this with something that
uses the count, default and input box to create the list without dups
on the first try. I tried looking through the site for a solution but
the stuff that looked like it might be what I wanted I couldn't figure
out how to add my variables to at all. Thank you in advance for any
assistance.

Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Dim reccount As Double
Dim recdefault As Double
Dim trunrecdef As Integer
Dim recpct As Double

reccount = Selection.Count
'counts records to determine upperbound
recdefault = 117
'default number of random numbers required
trunrecdef = Fix(recdefault)
'this step gives whole # if math on default
Do
recpct = Application.InputBox(Prompt:=("How many PRV lots to
keep" & vbLf & "Max lots allowed " &_ reccount),Title:="Number PRV
lots to test", Default:=trunrecdef, Type:=1)
Loop Until recpct <= reccount
'input number of random numbers, wont take #
higher than reccount
ActiveWindow.Close
'closes file count was performed on
Workbooks.Open Filename:= _
"T:\Groups\PAM\Meat Juice Lab\Processing lab B7\Current
selective\s_5 to 2 PSS\N_RandID.xls"
'file rand # list goes on
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
Range("A2").Select
ActiveCell.FormulaR1C1 = "=RANDBETWEEN(1," & reccount & ")"
' sets randbetween to 1-count of records
Selection.Copy
ActiveCell.Offset(1, 0).Select
StartCell = ActiveCell.Offset(0, 0).Address
EndCell = ActiveCell.Offset(recpct - 2, 0).Address
Range(StartCell, EndCell).Select
'selects range to copy randbetween to
ActiveSheet.Paste

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Variable count and bound random numbers

See:

Getting An Array Of Unique, Non-Duplicated Values

in:

http://www.cpearson.com/excel/randomnumbers.aspx
--
Gary''s Student - gsnu200761


"goaljohnbill" wrote:

I need to create a variable sized list of random numbers with a
variable sized range without duplicate values. I have been creating
the list with a variable range with the following code then manually
changing any dup values (checking changes by sorting the list and
filtering uniques). I would like to replace this with something that
uses the count, default and input box to create the list without dups
on the first try. I tried looking through the site for a solution but
the stuff that looked like it might be what I wanted I couldn't figure
out how to add my variables to at all. Thank you in advance for any
assistance.

Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Dim reccount As Double
Dim recdefault As Double
Dim trunrecdef As Integer
Dim recpct As Double

reccount = Selection.Count
'counts records to determine upperbound
recdefault = 117
'default number of random numbers required
trunrecdef = Fix(recdefault)
'this step gives whole # if math on default
Do
recpct = Application.InputBox(Prompt:=("How many PRV lots to
keep" & vbLf & "Max lots allowed " &_ reccount),Title:="Number PRV
lots to test", Default:=trunrecdef, Type:=1)
Loop Until recpct <= reccount
'input number of random numbers, wont take #
higher than reccount
ActiveWindow.Close
'closes file count was performed on
Workbooks.Open Filename:= _
"T:\Groups\PAM\Meat Juice Lab\Processing lab B7\Current
selective\s_5 to 2 PSS\N_RandID.xls"
'file rand # list goes on
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
Range("A2").Select
ActiveCell.FormulaR1C1 = "=RANDBETWEEN(1," & reccount & ")"
' sets randbetween to 1-count of records
Selection.Copy
ActiveCell.Offset(1, 0).Select
StartCell = ActiveCell.Offset(0, 0).Address
EndCell = ActiveCell.Offset(recpct - 2, 0).Address
Range(StartCell, EndCell).Select
'selects range to copy randbetween to
ActiveSheet.Paste

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default Variable count and bound random numbers

Thanks for the help guys. What would i do to get joels code snippet to
give whole numbers instead of decimals?
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
Create a Random Variable [email protected] Excel Discussion (Misc queries) 3 August 25th 09 09:18 AM
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


All times are GMT +1. The time now is 01:12 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"