#Include <Array.au3>
$aRandom = _RandomEx(1, 3, 3, 1, 1)
_ArrayDisplay($aRandom)
; #FUNCTION# ==============================================================================================================
; Name...........: _RandomEx
; Description ...: Function generates a random numbers and displays them in a specified format.
; Syntax.........: _RandomEx($_iSNum, $_iENum, $_iRNumCount, $_iRetFormat, $_sRetDelimiter, $_iUnique)
; Parameters ....: $_iSNum - The smallest number to be generated. The default is 0.
; $_iENum - The largest number to be generated. The default is 1.
; $_iRNumCount - Quantity of random numbers
; $_iRetFormat - Return format (0 = String (Default), 1 = Array)
; $_sRetDelimiter - Random numbers delimeter if return format is string. The default is ","
; $_iUnique - Specified if generated random numbers should be unique (0 = not unique, 1 = unique (Default))
; Return values .: Success - Return random numbers between $_iSNum and $_iENum in specified format.
; Failure - 0, sets @error Returns 0 and sets @error flag to 1 if bad parameters
; |1 - $_iSNum equally or greater $_iENum
; |2 - Quantity of possible unique random numbers is less requested
; Author ........: XpycT, idea of return in string format madmasles http://autoit-script.ru/index.php/topic,6344.msg44489.html#msg44489
; =========================================================================================================================
Func _RandomEx($_iSNum = 0, $_iENum = 1, $_iRNumCount = 1, $_iUnique = 1, $_iRetFormat = 0, $_sRetDelimiter = ",")
Local $sRNumStr = "`", $iNumCount = 0
If $_iSNum >= $_iENum Then Return SetError(1, 0, 0)
If $_iUnique And ($_iENum - $_iSNum + 1) < $_iRNumCount Then Return SetError(2, 0, 0)
While $iNumCount <> $_iRNumCount
$iRNum = Random($_iSNum, $_iENum, 1)
If $_iUnique = 1 Then
If Not StringInStr($sRNumStr, "`" & $iRNum & "`") Then
$sRNumStr &= $iRNum & "`"
$iNumCount += 1
EndIf
Else
$sRNumStr &= $iRNum & "`"
$iNumCount += 1
EndIf
WEnd
$sRNumStr = StringTrimLeft(StringTrimRight($sRNumStr, 1), 1)
If $_iRetFormat = 0 Then Return StringReplace($sRNumStr, "`", $_sRetDelimiter)
If $_iRetFormat = 1 Then Return StringSplit($sRNumStr, "`")
EndFunc