Welcome to the Excel Forum

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed.

Please Register to Remove these Ads

Please Register to Remove these Ads



Reply
  #1  
Old 07-09-2009, 11:38 PM
nguyennb nguyennb is offline
Registered User
 
Join Date: 10 Apr 2009
Location: VietNam
MS Office Version:Excel 2003
Posts: 11
nguyennb is becoming part of the community
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 ?
Reply With Quote
  #2  
Old 07-10-2009, 12:22 AM
rylo rylo is offline
Forum Moderator
 
Join Date: 15 Jan 2007
Location: Brisbane, Australia
MS Office Version:2003
Posts: 4,027
rylo is very confident of their ability rylo is very confident of their ability rylo is very confident of their ability
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
Reply With Quote
  #3  
Old 07-10-2009, 05:26 AM
Phil_V Phil_V is offline
Valued Forum Contributor
 
Join Date: 23 Feb 2006
Location: Near London, England
MS Office Version:Office 2003
Posts: 752
Phil_V is attaining expert status Phil_V is attaining expert status
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
Reply With Quote
  #4  
Old 07-13-2009, 06:38 AM
nguyennb nguyennb is offline
Registered User
 
Join Date: 10 Apr 2009
Location: VietNam
MS Office Version:Excel 2003
Posts: 11
nguyennb is becoming part of the community
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 06:41 AM.
Reply With Quote


Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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

Forum Jump