## Random number generator

Moderator: DosItHelp

Hello DosTips-Users ^^ First, thanks for everyone, who helped me in the past weeks
So, my problem is: I need a random number generator. This number should be 32 numbers long. How do i do this? I couldn't make it myself :/
Thanks c:
Delayed expansion and a FOR /L loop will do the trick together with the dynamic random variable. In every iteration only the last digit of random will be appended to variable rnd.

Code: Select all

`@echo off &setlocalsetlocal EnableDelayedExpansionset "rnd="for /l %%i in (1 1 32) do set "rnd=!rnd!!random:~-1!"endlocal &set "rnd=%rnd%"echo %rnd%pause`

Variable rnd may contain leading zeros.

Steffen
Tami wrote:This number should be 32 numbers long.
Do you mean:
(1) "This number should be 32 bits long."
(2) "This number should be 32 (decimal) digits long.".

Do you need a signed, or an unsigned number - with or without trailing zeroes, evenly distributed, ...?

Code: Select all

`@echo offsetlocal enableExtensions enableDelayedExpansion:: 32 bit, signed, no trailing zeroes, evenly distributedset /A "rnd=(((!random!<<15)|!random!)<<15)|!random!"echo(rnd_1   = !rnd!:: 32 digits, unsigned, trailing zeroes, evenly distributedset "rnd="for /l %%a in (1, 1, 8) do (   set "part=0000!random:~-4!"   set "rnd=!rnd!!part:~-4!")echo(rnd_2.1 = !rnd!:: up to 32 digits, unsigned, no trailing zeroes, evenly distributed - additional stepfor /f "tokens=* delims=0" %%a in ("!rnd!") do set "rnd=%%~a"echo(rnd_2.2 = !rnd!:: up to 32 digits, signed, no trailing zeroes, evenly distributed - additional stepset /A "signBit=!random!&1"if !signBit! == 1 set "rnd=-!rnd!"echo(rnd_2.3 = !rnd!endlocalgoto :eof`

penpen
Thanks very much, it worked^^
penpen wrote:
Tami wrote:This number should be 32 numbers long.
Do you mean:
(1) "This number should be 32 bits long."
(2) "This number should be 32 (decimal) digits long.".

Do you need a signed, or an unsigned number - with or without trailing zeroes, evenly distributed, ...?

Code: Select all

`@echo offsetlocal enableExtensions enableDelayedExpansion:: 32 bit, signed, no trailing zeroes, evenly distributedset /A "rnd=(((!random!<<15)|!random!)<<15)|!random!"echo(rnd_1   = !rnd!:: 32 digits, unsigned, trailing zeroes, evenly distributedset "rnd="for /l %%a in (1, 1, 8) do (   set "part=0000!random:~-4!"   set "rnd=!rnd!!part:~-4!")echo(rnd_2.1 = !rnd!:: up to 32 digits, unsigned, no trailing zeroes, evenly distributed - additional stepfor /f "tokens=* delims=0" %%a in ("!rnd!") do set "rnd=%%~a"echo(rnd_2.2 = !rnd!:: up to 32 digits, signed, no trailing zeroes, evenly distributed - additional stepset /A "signBit=!random!&1"if !signBit! == 1 set "rnd=-!rnd!"echo(rnd_2.3 = !rnd!endlocalgoto :eof`

penpen

Now. penpen.
I was wondering could you possibly make something like this. Randomly generates a 32 digit number. But displays 1 number every second which means it takes 32 seconds to display the number. Yes I know this sounds sorta dumb but this would just make it a lot more cooler.
You could use "set/P" to display single digits, and "timeout" to delay the next digit by ~1 second:

Code: Select all

`@echo offsetlocal enableExtensions enableDelayedExpansionset "rnd="for /l %%a in (1, 1, 8) do (   set "part=0000!random:~-4!"   set "rnd=!rnd!!part:~-4!")<nul set /P "=!rnd:~0,1!"for /l %%a in (1, 1, 31) do (   >nul timeout /T 1 /NOBREAK   <nul set /P "=!rnd:~%%~a,1!")endlocalgoto :eof`

penpen
Sort by