# Random()

Go to solution Solved by Yashied,

## Recommended Posts

Hi all,

sometimes when I'm generating two random numbers just after another using the Random() function, I'm getting the same values.

This happens far too often to be random.

Has anybody else experienced this? Is there any way to avoid it without adding a sleep between the two randoms?

##### Share on other sites

I havent experienced this but it is possible. Its maybe more likely that there is a coding error. You could make an if statement that says:

```Global \$LastRandom

While 1
Local \$Random = Random(0,10,1)
If \$LastRandom == \$Random Then
Do
\$Random = Random(0,10,1)
Until \$LastRandom <> \$Random
MsgBox(0,"Random Number",\$Random)
Else
MsgBox(0,"Random Number",\$Random)
EndIf
\$LastRandom = \$Random
WEnd```

Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

##### Share on other sites

If you try to avoid the same number repeating, then you are avoiding the random chance that the same number will repeat. If you run Random(1, 10, 1) 1000 times, each number (1-10) will occur on average about 100 times - in any order.

Edited by czardas
##### Share on other sites

When I ran random I was getting the same number in a row around 1 in 10-15 tries. I didn't see the issue you are having. Can you provide some code?

Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

##### Share on other sites

• Solution

Try to use SRandom() at the beginning of your script:

`SRandom(@AutoItPID)`
##### Share on other sites

I made this with the help of a moderator to show the results of random:

```#include <GUIConstantsEx.au3>

Global \$aLabel[11], \$bStart = False

\$hGUI = GUICreate("Form1", 157, 438, 336, 218)
GUICtrlCreateLabel("Random Number Count", 16, 16, 115, 17)
For \$i = 1 To 10
GUICtrlCreateLabel("1", 24, 152 + (24 * \$i), 10, 17)
\$aLabel[\$i] = GUICtrlCreateLabel(0, 64, 152 + (24 * \$i), 30, 17)
Next
GUICtrlCreateLabel("Speed", 32, 120, 35, 17)
\$cSpeed = GUICtrlCreateInput(100, 24, 136, 49, 21)
\$cStart = GUICtrlCreateButton("Start", 24, 48, 89, 25)

\$ButtonResetCount = GUICtrlCreateButton("Reset Count", 24, 80, 89, 25)
GUISetState(@SW_SHOW)

\$nTimeStamp = TimerInit()

While 1
Switch GUIGetMsg()
Case \$GUI_EVENT_CLOSE
Exit
Case \$cStart
Case "Start"
GUICtrlSetData(\$cStart, "Stop")
\$bStart = True
Case Else
GUICtrlSetData(\$cStart, "Start")
\$bStart = False
EndSwitch
Case \$ButtonResetCount
ResetCount()
EndSwitch

If \$bStart Then
\$iRandom = Random(1, 10, 1)
\$nTimeStamp = TimerInit()
EndIf
EndIf

WEnd

Func ResetCount()
For \$i = 1 To 10
GUICtrlSetData(\$aLabel[\$i], 0)
Next
\$bStart = False
EndFunc   ;==>ResetCount```

Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

##### Share on other sites

If you try to avoid the same number repeating, then you are avoiding the random chance that the same number will repeat. If you run Random(1, 10, 1) 1000 times, each number (1-10) will occur on average about 100 times - in any order.

I am aware of this. Since the numbers I use are usually too big and I get this result pretty often it's hard to believe that it's still random.

Sorry. I forgot to add that I'm only having this issue when running more than one script on random.

Check this out:

This is just an example I did a minute ago.

I've got a server software launching other AutoIt scripts, using asynchronous communication between them.

They get identified by a longer number I'm rolling at random when the "child"script starts.

When two of them happen to get launched directly after anouther they get the same ID and I get really weird results.

Edit:

I made this with the help of a moderator to show the results of random

This is really awesome to look at.

##### Share on other sites

I made this with the help of a moderator to show the results of random:

It is the original code of MELBA23

And you even copied the error in line 8:

`GUICtrlCreateLabel(1, 24, 152 + (24 * \$i), 10, 17)`

should be

`GUICtrlCreateLabel(\$i, 24, 152 + (24 * \$i), 10, 17)`

App: Au3toCmd              UDF: _SingleScript()

##### Share on other sites

Wow.... I made the idea, I made the original faulty code, I pursued the right answer. I ran it and it worked. I made it to share in this post and I even gave props to the moderator for helping. What's the big deal here? Rest assured Im not getting paid for my efforts nor am I hiding anything.

When you are running 2 of these back to back you are probably doing so withing the same second (This is a guess). Try adding sleep(1000) between those and see if the results are different. From what I have heard there is no real random in computer language. The program is probably taking the date and time and running it through an algorithm and displaying the results. If you change a variable it should change the output.

Edited by computergroove

Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

##### Share on other sites

Wow.... I made the idea, I made the original faulty code, I pursued the right answer. I ran it and it worked. I made it to share in this post and I even gave props to the moderator for helping. What's the big deal here? Rest assured Im not getting paid for my efforts nor am I hiding anything.

When you are running 2 of these back to back you are probably doing so withing the same second (This is a guess). Try adding sleep(1000) between those and see if the results are different. From what I have heard there is no real random in computer language. The program is probably taking the date and time and running it through an algorithm and displaying the results. If you change a variable it should change the output.

A link to the topic would have been just fine.

Back on topic, is the goal to run more than one script using random at the same time?

Snips & Scripts

My Snips: graphCPUTemp ~ getENVvars
My Scripts: message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

##### Share on other sites

• Moderators

@Exit and @computergrove, play nice.

@Radiance, Yashield gave you what you needed in post #5, you need a seed basically.

<snip>, cute, I wrote out basically what SRandom said in the help file before I thought that it was probably explained there!.... Just see the helpfile lol

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 forgot to add that I'm only having this issue when running more than one script on random.

Interesting discovery.

##### Share on other sites

• Moderators

Hi,

I knew this had been discussed before, but it took a while to find it.

M23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

##### Share on other sites

As it had been said, use SRandom, so you can set a seed. Probably Autoit automatically uses time() function as a seed, which can cause such problems. If you choose a better Seed (like the previously mentioned PID) you can avoid it.

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
×
• Create New...