# Random problem

## Recommended Posts

Hi I want to make 10 random numbers from 0 to 9. Problem is i don't want them to repeat it self.

example:

1. - 0 8 9 5 6 3 7 4 1 2

2. - 3 5 7 4 2 9 0 8 2 1

here is my code:

```FileOpen("123.txt", 1)

Dim \$array[10]

For \$i = 0 to 9

\$array[\$i] = Random(0, 9, 1)

For \$j = 0 To \$i

While \$array[\$j] = \$array[\$i]

\$array[\$i] = Random(0, 9, 1)

WEnd
Next

FileWrite("123.txt", \$array[\$i] & @CRLF)

Next```

Am I doing something wrong here?

##### Share on other sites

Mislis ovako:

Did you mean like this:

```FileOpen("123.txt",1)
Dim \$array[10]
For \$i = 0 to 9
\$array[\$i] = Random(0, 9, 1)
For \$j = 0 To \$i
While \$array[\$j] = \$array[\$i]
\$array[\$i] = Random(0, 9, 1)
FileWrite("123.txt", \$array[\$i] & @CRLF)            Exit
WEnd
Next
Next```

[quote name='dbzfanatic' post='609696' date='Nov 26 2008, 08:46 AM']This is a help forum not a "write this for me" forum.[/quote](Sorry for bad English) :)

##### Share on other sites

```#include <array.au3>
Global \$nRan, \$sHold, \$nCount
While \$nCount < 10
\$nRan = Random(0, 9, 1)
If StringInStr("," & \$sHold, "," & \$nRan & ",") = 0 Then
\$sHold &= \$nRan & ","
\$nCount += 1
EndIf
WEnd
MsgBox(0, "info", \$sHold)
;Make array
\$aSplit = StringSplit(StringTrimRight(\$sHold, 1), ",")
_ArrayDisplay(\$aSplit)```

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

##### Share on other sites

Or this:

```#include <Array.au3>

Dim \$sStart[10] = [0,1,2,3,4,5,6,7,8,9]
Dim \$sEnd[10]

For \$x = 0 to 9
;Randomly pick a number that is still in the array
\$i = Random(0,UBound(\$sStart)-1,1)
\$sEnd[\$x] = \$sStart[\$i]
_ArrayDelete(\$sStart,\$i)
Next

_ArrayDisplay(\$sEnd)```

This only uses the Random function 10 times.

##### Share on other sites

Mislis ovako:

Did you mean like this:

```FileOpen("123.txt",1)
Dim \$array[10]
For \$i = 0 to 9
\$array[\$i] = Random(0, 9, 1)
For \$j = 0 To \$i
While \$array[\$j] = \$array[\$i]
\$array[\$i] = Random(0, 9, 1)
FileWrite("123.txt", \$array[\$i] & @CRLF)            Exit
WEnd
Next
Next```
ne ba tako. triba upisati svih 10 brojeva pa izlaz nije ba opcija. ako makne exit onda sam opet na početku

not that way cause i need it to write all 10 numbers se exit is not an option. if you move exit then back to start and it writes only one first random number

##### Share on other sites

to jes :/

Edited by n3nE

[quote name='dbzfanatic' post='609696' date='Nov 26 2008, 08:46 AM']This is a help forum not a "write this for me" forum.[/quote](Sorry for bad English) :)

##### Share on other sites

Hi I want to make 10 random numbers from 0 to 9. Problem is i don't want them to repeat it self.

example:

1. - 0 8 9 5 6 3 7 4 1 2

2. - 3 5 7 4 2 9 0 8 2 1

here is my code:

Am I doing something wrong here?

The problem with that kind of loop is the repeated looping through the array. Work with a string, and only break it into an array when finished:

```#include <array.au3>

\$Timer = TimerInit()
Dim \$sData = ""
For \$i = 0 To 9
While 1
\$iData = Random(0, 9, 1)
If StringInStr(\$sData, String(\$iData)) Then
ContinueLoop
Else
\$sData &= "," & \$iData
ExitLoop
EndIf
WEnd
Next
\$avData = StringSplit(StringTrimLeft(\$sData, 1), ",")
_ArrayDisplay(\$avData, "Debug: Results in " & Round(TimerDiff(\$Timer) / 1000, 3) & " seconds.")```

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

##### Share on other sites

Evo kao sto je Smoke_N napisao ali malo izmenjeno i mislim da je to tebi potrebno:

```Global \$nRan, \$sHold, \$nCount
While \$nCount < 10
\$nRan = Random(0, 9, 1)
If StringInStr("," & \$sHold, "," & \$nRan & ",") = 0 Then
\$sHold &= \$nRan & @CRLF
\$nCount += 1
EndIf
WEnd
FileWrite("123.txt", \$sHold)```

Zar ne

[quote name='dbzfanatic' post='609696' date='Nov 26 2008, 08:46 AM']This is a help forum not a "write this for me" forum.[/quote](Sorry for bad English) :)

##### Share on other sites

The problem with that kind of loop is the repeated looping through the array. Work with a string, and only break it into an array when finished:

```#include <array.au3>

\$Timer = TimerInit()
Dim \$sData = ""
For \$i = 0 To 9
While 1
\$iData = Random(0, 9, 1)
If StringInStr(\$sData, String(\$iData)) Then
ContinueLoop
Else
\$sData &= "," & \$iData
ExitLoop
EndIf
WEnd
Next
\$avData = StringSplit(StringTrimLeft(\$sData, 1), ",")
_ArrayDisplay(\$avData, "Debug: Results in " & Round(TimerDiff(\$Timer) / 1000, 3) & " seconds.")```

I've tested all the methods a few time, but yours seems to be the fastest.. Always coming in at 1.5 seconds.

##### Share on other sites

Evo kao sto je Smoke_N napisao ali malo izmenjeno i mislim da je to tebi potrebno:

```Global \$nRan, \$sHold, \$nCount
While \$nCount < 10
\$nRan = Random(0, 9, 1)
If StringInStr("," & \$sHold, "," & \$nRan & ",") = 0 Then
\$sHold &= \$nRan & @CRLF
\$nCount += 1
EndIf
WEnd
FileWrite("123.txt", \$sHold)```

Zar ne

Boy the online translator really murdered that one!!!

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

##### Share on other sites

Boy the online translator really murdered that one!!!

Haha, I love it how it translates Smoke_N as Tuxedo!!!

`Evo as a hundred had Tuxedo written limit few izmenjeno plus thinker that it is you necessarily`

##### Share on other sites

lol

Evo kao sto je Smoke_N napisao ali malo izmenjeno i mislim da je to tebi potrebno=~

I think you need something like Smoke_N's code but with little changes

Edited by n3nE

[quote name='dbzfanatic' post='609696' date='Nov 26 2008, 08:46 AM']This is a help forum not a "write this for me" forum.[/quote](Sorry for bad English) :)

##### Share on other sites

lol

Evo kao sto je Smoke_N napisao ali malo izmenjeno i mislim da je to tebi potrebno=~

I think you need something like Smoke_N's code but with little changes

I would go with PsaltyDS' one.

##### Share on other sites

I would go with PsaltyDS' one.

I definately don't want to rain on a parade... but are you sure you tested that right? I keep going over it, and the extra function calls are much slower.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

##### Share on other sites

I removed the way I was doing mine to be similar to his ie...

\$sHold &= \$nRan & ","

To

\$sHold &= "," & \$nRan

Then changed StringTrimRight to StringTrimLeft .... and the speed doubled.

(I'll admit, I hadn't looked at that approach before... but it make sense)

(Had to change his up a bit to be able to handle more than 10 chars)

Now, if you run mine first:

```\$Timer1 = TimerInit()
Global \$nRan, \$sHold, \$nCount, \$sTemp
While \$nCount < 100
\$nRan = Random(0, 99, 1)
If StringInStr("," & \$sHold, "," & \$nRan & ",") = 0 Then
\$sHold &= "," & \$nRan
\$nCount += 1
EndIf
WEnd
\$aSplit = StringSplit(StringTrimLeft(\$sHold, 1), ",")
\$sTime =  "SmOke_N: " & TimerDiff(\$Timer1) & @CRLF
\$Timer2 = TimerInit()
Dim \$sData = ""
For \$i = 0 To 99
While 1
\$iData = Random(0, 99, 1)
If StringInStr("," & \$sData, "," & \$iData & ",") Then
ContinueLoop
Else
\$sData &= "," & \$iData
ExitLoop
EndIf
WEnd
Next
\$avData = StringSplit(StringTrimLeft(\$sData, 1), ",")
\$sTime &=  "PsaltyDS: " & TimerDiff(\$Timer2)
MsgBox(64, "info", \$sTime)```

You'll see the one ran first is slower always.... Is that how you were running them (his last)?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

##### Share on other sites

```#include <array.au3>
Global \$nRan, \$sHold, \$nCount
While \$nCount < 10
\$nRan = Random(0, 9, 1)
If StringInStr("," & \$sHold, "," & \$nRan & ",") = 0 Then
\$sHold &= \$nRan & ","
\$nCount += 1
EndIf
WEnd
MsgBox(0, "info", \$sHold)
;Make array
\$aSplit = StringSplit(StringTrimRight(\$sHold, 1), ",")
_ArrayDisplay(\$aSplit)```
I modified this version and it worked great for me. Thanks everybody, for the effort.

##### Share on other sites

Just had to add my own little contribution. In tests this method was faster than Smoke's, but I'm not sure if I tested properly...

```#include <Array.au3>
Local \$array[10] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
_ArrayShuffle(\$array)
_ArrayDisplay(\$array)

Func _ArrayShuffle(ByRef \$aArray, \$iBase = 0, \$iUBound = 0)
If \$iUBound = 0 Then \$iUBound = UBound(\$aArray) - 1
Local \$temp, \$rand
For \$i = \$iBase To \$iUBound
\$temp = \$aArray[\$i]
\$rand = Random(0, \$iUBound, 1)
\$aArray[\$i] = \$aArray[\$rand]
\$aArray[\$rand] = \$temp
Next
EndFunc```

##### Share on other sites

You'll see the one ran first is slower always.... Is that how you were running them (his last)?

When just running the first test, I first get a result where it says:

Smoke_N: 10.66...

PsaltyDS: 10.96

The next result is:

Smoke_N: 8.42

PsaltyDS: 10.61

The next result after that is:

Smoke_N: 10.66

PsaltyDS: 8.26

That seems pretty contradictory to what you say. In my tests, I was running both of the methods separately by the way.

##### Share on other sites

That seems pretty contradictory to what you say. In my tests, I was running both of the methods separately by the way.

This is a random sequence generator. The number of times it has to test a number each run will be... random. The depth it will have to search to disqualify a number will be... random.

If you plot all the times from a huge number of runs, I bet you get a nice statistical bell curve of standard distribution.

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...