+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    04-10-2009
    Location
    VietNam
    MS-Off Ver
    Excel 2003
    Posts
    11

    Create a random array contain a fixed number

    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 ?

  2. #2
    Forum Guru
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    5,015

    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

  3. #3
    Valued Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770

    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

  4. #4
    Registered User
    Join Date
    04-10-2009
    Location
    VietNam
    MS-Off Ver
    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 06:41 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0