Page 1 of 1

Random number generator

Posted: 08 Apr 2017 03:23
by Tami
Hello DosTips-Users ^^ First, thanks for everyone, who helped me in the past weeks :D
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:

Re: Random number generator

Posted: 08 Apr 2017 03:43
by aGerman
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 &setlocal

setlocal EnableDelayedExpansion
set "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

Re: Random number generator

Posted: 08 Apr 2017 05:38
by penpen
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 off
setlocal enableExtensions enableDelayedExpansion

:: 32 bit, signed, no trailing zeroes, evenly distributed
set /A "rnd=(((!random!<<15)|!random!)<<15)|!random!"
echo(rnd_1   = !rnd!


:: 32 digits, unsigned, trailing zeroes, evenly distributed
set "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 step
for /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 step
set /A "signBit=!random!&1"
if !signBit! == 1 set "rnd=-!rnd!"
echo(rnd_2.3 = !rnd!

endlocal
goto :eof


penpen

Re: Random number generator

Posted: 09 Apr 2017 03:29
by Tami
Thanks very much, it worked^^

Re: Random number generator

Posted: 03 Jun 2017 08:31
by SaXaMoD
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 off
setlocal enableExtensions enableDelayedExpansion

:: 32 bit, signed, no trailing zeroes, evenly distributed
set /A "rnd=(((!random!<<15)|!random!)<<15)|!random!"
echo(rnd_1   = !rnd!


:: 32 digits, unsigned, trailing zeroes, evenly distributed
set "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 step
for /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 step
set /A "signBit=!random!&1"
if !signBit! == 1 set "rnd=-!rnd!"
echo(rnd_2.3 = !rnd!

endlocal
goto :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.

Re: Random number generator

Posted: 03 Jun 2017 17:29
by penpen
You could use "set/P" to display single digits, and "timeout" to delay the next digit by ~1 second:

Code: Select all

@echo off
setlocal enableExtensions enableDelayedExpansion

set "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!"
)

endlocal
goto :eof


penpen