Randomly Insert All Array Elements Into a String..??

Recommended Posts

Say I have an Array of 10 Elements like so..

```Dim \$Array[10]
\$Array[0]="A"
\$Array[1]="B"
\$Array[2]="C"
\$Array[3]="D"
\$Array[4]="E"
\$Array[5]="F"
\$Array[6]="G"
\$Array[7]="H"
\$Array[8]="I"
\$Array[9]="J"```

And I want to Insert all of those Elements into a String by a Random Order.

```\$NormalString = "A B C D E F G H I J" <--Normal Order
\$NormalString = \$Array[0]&" "&\$Array[1]&" "&\$Array[2]&" "&\$Array[3]&" "&\$Array[4]&" "&\$Array[5]&" "&\$Array[6]&" "&\$Array[7]&" "&\$Array[8]&" "&\$Array[9]

\$RandomString = "E C I J H B G F A D" <--Random Order
\$RandomString = \$Array[Random(0, 9, 1)]&" "&\$Array[Random(0, 9, 1)]&" "&\$Array[Random(0, 9, 1)]&" "&\$Array[Random(0, 9, 1)]&" "&\$Array[Random(0, 9, 1)]&" "&\$Array[Random(0, 9, 1)]&" "&\$Array[Random(0, 9, 1)]&" "&\$Array[Random(0, 9, 1)]&" "&\$Array[Random(0, 9, 1)]&" "&\$Array[Random(0, 9, 1)]```

How do I make it so that each Array Element only gets randomly inserted once into the String..??

Share on other sites

well i know to randomize arrays you can use

Array Randomize Function

_ArrayRandomize(\$<variable>)

and than just implement it in a string

Share on other sites

is _ArrayRandomize() even a real function/UDF..??

Share on other sites

is _ArrayRandomize() even a real function/UDF..??

I just checked the help file and I don't think it's exist... try this:

```#include <Array.au3>

Dim \$aArray[6] = [1, 2, 3, 4, 5, 6]
_ArrayRandom(\$aArray)
_ArrayDisplay(\$aArray)

Func _ArrayRandom(ByRef \$a)
\$u = UBound(\$a) -1
For \$i = 0 To \$u
_ArraySwap(\$a[Random(0,\$u,1)], \$a[Random(0, \$u,1)])
Next
EndFunc```

you can use better algorithm, this one is just an example to show how it works...

Edited by Mison

Hi ;)

Share on other sites

Say I have an Array of 10 Elements like so..

```Dim \$Array[10]
\$Array[0]="A"
\$Array[1]="B"
\$Array[2]="C"
\$Array[3]="D"
\$Array[4]="E"
\$Array[5]="F"
\$Array[6]="G"
\$Array[7]="H"
\$Array[8]="I"
\$Array[9]="J"```

And I want to Insert all of those Elements into a String by a Random Order.

```\$NormalString = "A B C D E F G H I J" <--Normal Order
\$NormalString = \$Array[0]&" "&\$Array[1]&" "&\$Array[2]&" "&\$Array[3]&" "&\$Array[4]&" "&\$Array[5]&" "&\$Array[6]&" "&\$Array[7]&" "&\$Array[8]&" "&\$Array[9]

\$RandomString = "E C I J H B G F A D" <--Random Order
\$RandomString = \$Array[Random(0, 9, 1)]&" "&\$Array[Random(0, 9, 1)]&" "&\$Array[Random(0, 9, 1)]&" "&\$Array[Random(0, 9, 1)]&" "&\$Array[Random(0, 9, 1)]&" "&\$Array[Random(0, 9, 1)]&" "&\$Array[Random(0, 9, 1)]&" "&\$Array[Random(0, 9, 1)]&" "&\$Array[Random(0, 9, 1)]&" "&\$Array[Random(0, 9, 1)]```

How do I make it so that each Array Element only gets randomly inserted once into the String..??

This is one way of creating a random ordered string from a sorted array.

```;
#include <Array.au3>

Local \$Array[10]
\$Array[0]="A"
\$Array[1]="B"
\$Array[2]="C"
\$Array[3]="D"
\$Array[4]="E"
\$Array[5]="F"
\$Array[6]="G"
\$Array[7]="H"
\$Array[8]="I"
\$Array[9]="J"

_ArrayDisplay(\$Array)

Local \$x = -1, \$RandomString = ""

While \$x <> UBound(\$Array) - 1
\$iRandNum = Random(0, 9, 1)
If StringInStr(\$RandomString, \$Array[\$iRandNum]) = 0 Then
\$RandomString &= \$Array[\$iRandNum] & " "
\$x += 1
EndIf
WEnd

;ConsoleWrite(\$x & "  " & \$RandomString & @CRLF)
MsgBox(0, "Random String", \$RandomString)
;```
Share on other sites

• 3 weeks later...

This is one way of creating a random ordered string from a sorted array.

```;
#include <Array.au3>

Local \$Array[10]
\$Array[0]="A"
\$Array[1]="B"
\$Array[2]="C"
\$Array[3]="D"
\$Array[4]="E"
\$Array[5]="F"
\$Array[6]="G"
\$Array[7]="H"
\$Array[8]="I"
\$Array[9]="J"

_ArrayDisplay(\$Array)

Local \$x = -1, \$RandomString = ""

While \$x <> UBound(\$Array) - 1
\$iRandNum = Random(0, 9, 1)
If StringInStr(\$RandomString, \$Array[\$iRandNum]) = 0 Then
\$RandomString &= \$Array[\$iRandNum] & " "
\$x += 1
EndIf
WEnd

;ConsoleWrite(\$x & "  " & \$RandomString & @CRLF)
MsgBox(0, "Random String", \$RandomString)
;```

I modified your code (thanks) to suit my own need for a randomizeArray function. I am randomizing an array of filenames, but it can be used for any type of 1 based array.

```func RandomizeArray(byRef \$array)
local \$numItems = \$array[0]
local \$tempArray[\$numItems+1]
\$tempArray[0] = \$numItems

Local \$x = 1, \$RandomString = ""

while \$x <= \$numItems
\$iRandNum = Random(1, \$numItems, 1)
;ConsoleWrite(\$iRandNum & @CRLF)
If StringInStr(\$RandomString, " - " & \$iRandNum & " - ") = 0 Then   ;using this format to avoid incorrect matches
;ConsoleWrite("x: " & \$x & " \$numItems: " & \$tempArray[0] & @CRLF)
\$tempArray[\$x] = \$array[\$iRandNum]
\$RandomString &= " - " & \$iRandNum & " - "
\$x += 1
;ConsoleWrite(\$RandomString & " " & \$array[\$iRandNum] & " x: " & \$x &  " iRandom: " & \$iRandNum  & @CRLF)
EndIf
WEnd

\$array = \$tempArray
EndFunc```

I haven't done extensive performance testing but it loops between 100 and 200 times in general in randomizing 30 items.

Is there a better way for randomizing an array?

D

Share on other sites

....

Is there a better way for randomizing an array?

D

Randomizing an array could be called shuffling an array.

Here is another.

```;
#include <Array.au3>
;; http://www.autoitscript.com/forum/index.php?showtopic=103429&view=findpost&p=739291
Local \$Array[10], \$aShuffledArr
\$Array[0] = "A"
\$Array[1] = "B"
\$Array[2] = "C"
\$Array[3] = "D"
\$Array[4] = "E"
\$Array[5] = "F"
\$Array[6] = "G"
\$Array[7] = "H"
\$Array[8] = "I"
\$Array[9] = "J"

;Will not include array[0] in shuffle, with \$iStartShuffIndx set to 1.
\$aShuffledArr = _ArrayShuffle(\$Array, 1, 2)

_ArrayDisplay(\$aShuffledArr)
ConsoleWrite(_ArrayToString(\$aShuffledArr, @CRLF, 1) & @CRLF); Start string at array index 1

Func _ArrayShuffle(\$Array, \$iStartShuffIndx = 0, \$iNumOfShuffles = 2)
Local \$iSize = UBound(\$Array), \$s2, \$i

;shuffle array
For \$n = 1 To \$iNumOfShuffles
For \$i = \$iStartShuffIndx To \$iSize - 1
Do
\$s2 = Random(\$iStartShuffIndx, \$iSize - 1, 1)
Until \$i <> \$s2
_ArraySwap(\$Array[\$i], \$Array[\$s2])
Next
Next
Return \$Array
EndFunc ;==>_ArrayShuffle```

Edit: Slightly modified function - more efficient.

Edit2: Ok, I think I've got it this time.

Edited by Malkey

Create an account

Register a new account