Что нового

Сгенерировать случайное число, исключая существующие комбинации

DesMono

Новичок
Сообщения
19
Репутация
0
Имеется текстовый файл (file.txt) следующего содержания:

843-65-54
285-69-75
996-32-95
474-57-68
258-78-89
936-26-82
259-92-96
...
Нужно сгенерировать случайное число такого же вида (xxx-xx-xx), но исключая существующие комбинации в файле.
По-моему тут что-то очень не так :smile:
Код:
$file = FileOpen('D:\file.txt', 0)
$read = FileRead($file)
$rText = StringRegExpReplace($read, '-', '')
$aLines = StringSplit($rText, @CRLF, 1)

$iRandom = Random(1000000, 9999999, 1)

For	$i = 1 To $aLines[0] Step +1
    If StringInStr($aLines[$i], $iRandom) Then ContinueLoop
Next
MsgBox (0, 'Result', $iRandom)

Кстати, как добавить полученному числу разделители '-' ?
 

WSWR

AutoIT Гуру
Сообщения
941
Репутация
363
DesMono

Можно так попробовать:

Код:
#include <String.au3>
MsgBox(0, 'Result', _Random())

Func _Random()
	$file = FileOpen('D:\file.txt', 0)
	$read = FileRead($file)
	$rText = StringReplace($read, '-', '')
	While 1
		$iRandom = Random(1000000, 9999999, 1)
		If Not StringInStr($rText, $iRandom) Then ExitLoop
	WEnd
	FileClose($file)	
	$iRandom = _StringInsert(String($iRandom), '-', 3)
	$iRandom = _StringInsert(String($iRandom), '-', -2)
	Return $iRandom
EndFunc   ;==>_Random
 
Верх