How to generate random numbers at the command prompt?

1

I have a file in .cmd and I need it to generate random numbers with 10 algorithms. Such as 9006100001 and 1579970319 generated randomly.

I currently have the following function generating random numbers based on the system time.

setlocal 
for /f "skip=8 tokens=2,3,4,5,6,7,8 delims=: " %%D in ('robocopy /l * \ \ /ns /nc /ndl /nfl /np /njh /XF * /XD *') do (
 set "dow=%%D"
 set "month=%%E"
 set "day=%%F"
 set "HH=%%G"
 set "MM=%%H"
 set "SS=%%I"
 set "year=%%J"
)

From this function, I only use %SS% , it's the closest I got to what I want so far, how could I create something similar to generate random numbers?

If possible, you could include a variation of this with alphanumeric characters *

    
asked by anonymous 17.10.2016 / 03:38

2 answers

1

Another alternative is to create an alphabet with the characters you want to generate, in a loop for you use the random to randomly get an index and access the value of a array :

@echo off
setlocal enabledelayedexpansion

set "alfabeto=0 1 2 3 4 5 6 7 8 9"

set "tamanho=0"
set "resultado="

REM Cria um array com os elementos
for %%a in (%alfabeto%) do (
    set "!tamanho!=%%a"
    set /a "tamanho+=1"
)

REM Retorna 10 caracteres aleatórios
for /L %%G in (1 1 10) do (
    set /a "indice=!random! %% tamanho"
    for %%b in (!indice!) do set "resultado=!resultado!!%%b!"
)

echo %resultado%
endlocal
  

If possible, could you include a variation of this with alphanumeric characters?

Using the example above, include the characters in the alphabet:

set "alfabeto=a b c d f g h i j k l m n p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9"
    
17.10.2016 / 04:47
2

With your function already generating random numbers, take the result and multiply it by a large number, and as long as this number does not exceed 10 digits, keep multiplying. EX:

@echo off
mode 76,30
color 9f
title Gerador de numeros aleatorios com mais de 10 algarimos
setlocal enabledelayedexpansion

:MAIN
cls 
echo.
echo Gerador de numeros aleatorios.
echo.
set start=s
set /p start=Iniciar(s/n):
if %start% equ s call :gerar && goto multiplicar
if not %start% equ s goto MAIN
goto MAIN

:gerar
set gerar=%random%

:multiplicar
set /A gerar=%gerar%*9876
if not %gerar% gtr 999999999 goto multiplicar
if %gerar% gtr 999999999 goto n_gerado

:n_gerado
echo.
echo nº gerado: %gerar%
pause>nul
goto MAIN

I hope it helps.

    
17.10.2016 / 04:14