Forum Statistics
- Forum Members:
- Total Threads:
- Total Posts: 4
There are 1 users currently browsing forums.
|
 |

07-09-2009, 10:38 PM
|
|
Registered User
|
|
Join Date: 10 Apr 2009
Location: VietNam
MS Office Version:Excel 2003
Posts: 11
|
|
|
Create a random array contain a fixed number
Please Register to Remove these Ads
I want to create an array contain random 4 number, but one of them is a fixed number.
For example, fixed number is 2, arrays are 1,2,5,9; 2,6,7,8; 2,5,9,15..
.The numbers are between 1 and 100.
How can I do it ?
|

07-09-2009, 11:22 PM
|
|
Forum Moderator
|
|
Join Date: 15 Jan 2007
Location: Brisbane, Australia
MS Office Version:2003
Posts: 4,096
|
|
|
Re: Create a random array contain a fixed number
Hi
Maybe this will get you going.
Code:
Sub aaa()
Dim arr(3)
arr(0) = 2
For i = 1 To 3
arr(i) = Int(Rnd() * 100)
Next i
For i = 0 To 3
Range("E1").Offset(i, 0).Value = arr(i)
Next i
End Sub
rylo
|

07-10-2009, 04:26 AM
|
|
Valued Forum Contributor
|
|
Join Date: 23 Feb 2006
Location: Near London, England
MS Office Version:Office 2003
Posts: 752
|
|
|
Re: Create a random array contain a fixed number
I don't know if the placement of the '2' has to be random with the array, but if so the above code will not do it.
Below is a flexible solution that will prompt you for inputs, (most are defaulted to the values in your original post).
Hopefully you will be able to see how it works, but if not feel free to ask questions:
Code:
Sub random_arrays_with_fix()
Dim thearray() As Long
Dim input_reply As Variant
Dim upper_bound As Long, fixed_num As Long, array_elements As Long, qty_of_arrays As Long
Dim counter As Long, arr_index As Long
' Clear the worksheet first
ActiveSheet.Cells.ClearContents
' Get the highest value allowed for the array
Do
input_reply = InputBox("Enter the highest value that can be in the array", "Highest value", "100")
If input_reply = "" Then Exit Sub Else upper_bound = input_reply
Loop While Not IsNumeric(upper_bound) Or upper_bound < 1
' Get the 'fixed' value for the array
Do
input_reply = InputBox("Enter the 'fixed' number:", "Fixed Number", "2")
If input_reply = "" Then Exit Sub Else fixed_num = input_reply
Loop While (Not IsNumeric(fixed_num)) Or fixed_num > upper_bound
' How many elements in the array?
Do
input_reply = InputBox("How many elements are required in the array:", "Number of elements", "4")
If input_reply = "" Then Exit Sub Else array_elements = input_reply
Loop While Not IsNumeric(array_elements) Or array_elements < 1
' How many arrays to generate?
Do
input_reply = InputBox("How many arrays should be generated?", "Quantity of Arrays", "5")
If input_reply = "" Then Exit Sub Else qty_of_arrays = input_reply
Loop While Not IsNumeric(qty_of_arrays) Or qty_of_arrays < 1
' Generate the arrays
ReDim thearray(array_elements - 1)
For counter = 1 To qty_of_arrays
' Fill it with random numbers
For arr_index = 0 To UBound(thearray)
thearray(arr_index) = Int(Rnd() * upper_bound)
Next
thearray(Int(Rnd() * (array_elements - 1))) = fixed_num
' Paste the array into a row on the worksheet
Range("A" & counter).Resize(columnsize:=UBound(thearray) + 1) = thearray
Next
End Sub
__________________
If you find the response helpful please click the scales in the blue bar above and rate it 
If you don't like the response, don't bother with the scales, they are not for you
|

07-13-2009, 05:38 AM
|
|
Registered User
|
|
Join Date: 10 Apr 2009
Location: VietNam
MS Office Version:Excel 2003
Posts: 11
|
|
|
Re: Create a random array contain a fixed number
Yes. The placement of the '2' has to be random with the array. For example
1,2,5,56
2,8,9,15
3,7,16,2
5,6,2,9
And the number of array have to difference.
Last edited by nguyennb; 07-13-2009 at 05:41 AM.
|
 |
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|