Что нового

Как случайно перемешать числа от 1 до N?

snoitaleR

AutoIT Гуру
Сообщения
855
Репутация
223
Подскажите, пожалуйста, как перемешать числа от 1 до N?
У меня есть вариант, но он кажется громоздким..
Тем более, не решена проблема с последним элементом...
Код:
; НАЧАЛО

 $INI="random.ini"

 $N=200
 $L="|"

 For $I=1 To $N
  $L&=$I&"|"
 Next

 For $I=1 To $N
  $K=Random(1,$N-$I+1,1)
  $P1=StringInStr($L,"|",0,$K)
  $P2=StringInStr($L,"|",0,$K+1)
  $R=StringMid($L,$P1+1,$P2-$P1-1)
  $L1=StringMid($L,1,$P1-1)
  $L2=StringMid($L,$P2,StringLen($L))
  $L=$L1&$L2
  IniWrite($INI,"Picture",$R,0)
 Next

; КОНЕЦ
 

XpycT

Скриптер
Сообщения
380
Репутация
133
snoitaleR
Можно воспользоваться моей полезняшкой _RandomEx

Код:
$INI="random.ini"

$N=200

$aRandom = _RandomEx(1, $N, $N, 1, 1)

For $i = 0 To UBound($aRandom, 1) - 1
    IniWrite($INI, "Picture", $aRandom[$i], 0)
Next

; #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
 
Верх