Skip to content

Instantly share code, notes, and snippets.

@ayweak
Last active August 29, 2015 13:57
Show Gist options
  • Save ayweak/9647989 to your computer and use it in GitHub Desktop.
Save ayweak/9647989 to your computer and use it in GitHub Desktop.
%RANDOM% は 0 から 32767 (2^15 - 1) まで。 %ERRORLEVEL% は -2147483648 (2^31) から 2147483647 (2^31 - 1) まで。 (Windows 7 32bit)
@echo off
rem -*- coding: shift_jis-dos -*-
call :MAIN
exit /b
:MAIN
setlocal enabledelayedexpansion
for /l %%i in (-1, 1, 10) do (
call :RANDOM_N %%i
set n=!ERRORLEVEL!
if !n! neq -1 (
set z=
for /l %%j in (1, 1, %%i) do (
set z=0!z!
)
set n=!z!!n!
set n=!n:~-%%i!
)
echo %%i : !n!
)
exit /b
endlocal
:RANDOM_N
setlocal enabledelayedexpansion
if %1 leq 0 (
exit /b -1
) else if %1 geq 10 (
exit /b -1
)
call :RANDOM_4
set n=000%ERRORLEVEL%
for /l %%i in (4, 4, %1) do (
call :RANDOM_4
set n=000!ERRORLEVEL!!n:~-%%i!
)
set n=!n:~%1!
exit /b %n%
endlocal
:RANDOM_4
setlocal
set /a n=(%RANDOM% * 10000) / 32768
exit /b %n%
endlocal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment