Random number generator

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Tami
Posts: 7
Joined: 31 Mar 2017 11:01

Random number generator

#1 Post by Tami » 08 Apr 2017 03:23

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:

aGerman
Expert
Posts: 3178
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Random number generator

#2 Post by aGerman » 08 Apr 2017 03:43

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

penpen
Expert
Posts: 1483
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Random number generator

#3 Post by penpen » 08 Apr 2017 05:38

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

Tami
Posts: 7
Joined: 31 Mar 2017 11:01

Re: Random number generator

#4 Post by Tami » 09 Apr 2017 03:29

Thanks very much, it worked^^

SaXaMoD
Posts: 1
Joined: 03 Jun 2017 07:59

Re: Random number generator

#5 Post by SaXaMoD » 03 Jun 2017 08:31

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.

penpen
Expert
Posts: 1483
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Random number generator

#6 Post by penpen » 03 Jun 2017 17:29

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

Post Reply