I have done this sort of thing in the past, but don’t have the actual code accessible just now.
Here is the pseudocode:
define an array with values 0 to 9
Select an element of the array at random, place it into a holding variable (String)
Remove that element from the array, leaving an array of 8 values
Repeat this process until you have removed all elements from the array and they are now stored in the holding variable in random order.
then split the variable containing th 9 digit number into three.
Var numbers() As Integer
numbers = Array(1,2,3,4,5,6,7,8,9)
numbers.Shuffle // neat way to randomise the array
Dim newString As String = “”
For i As Integer = numbers.FirstIndex To numbers.LastIndex
If i = 3 Or i = 6 Then
newString = newstring + “,”
newString = newstring + numbers(i).toString
Else
newString = newstring + numbers(i).toString
End If
Next
Dave implied he wanted ALL posible combinations, trying to get all of them by just random chance and comparing if you already have that one is the worst brute force method, could take an eternity.
jjr already posted in xojo code the best algoritm known to solve this.
I agree :trying to get all of them by just random chance and comparing if you already have that one is the worst brute force method"
The method described doesn’t work as you describe. Run the code. You can put it into a loop and run it as many times as you like. What it does is randomly allocate the numbers from 1 to 9 to different positions in an array, then place them into a string with the three groups separated by a comma.
No number is repeated. It is a common fallacy to think that random allocation is the same thing as randomisation. There is a subtle, but very useful difference.
Public Function ShuffleDigits() as String
Var digits() As String = Array("1", "2", "3", "4", "5", "6", "7", "8", "9")
Var result As String
For i As Integer = 0 To 8
Var index As Integer = System.Random.InRange(0, digits.LastRowIndex)
result = result + digits(index)
If i = 2 Or i = 5 Then
result = result + "."
End
digits.RemoveRowAt(index)
Next
Return result
End Function
For i As Integer = 0 To 1000
System.DebugLog App.ShuffleDigits()
Next
Hi Ivan,
On reflection, I misinterpreted Dave’s implied request. Thank you. for pointing out my error. It pays to read the problem more carefully!
Still, the question posed has jumpstarted an application I have been mulling over for some time. The approach I have taken enables random distribution of patients into different treatment arms of the d=smae size. Solves a problem in statistics where one tries to randomly allocate people into different treatment arms and then finds there are different numbers in each arm.