@echo off
set scale=10000
echo. 利用随机数获取1/x的值.
echo. 下限: 1 (不包含), 上限: 32767 (不包含)
:loop
set /p input=请输入x :
call :test %input% %scale%
echo. 1/%input% 约等于 0.%zero%%errorlevel%
echo.&pause>nul&goto :loop
:length
setlocal enabledelayedexpansion
set "a=%~1"&set n=0
:length_loop
if "!a!"=="" (endlocal&exit /b %n%)
set "a=!a:~0,-1!"&set /a "n=!n!+1"
goto :length_loop
:test
setlocal enabledelayedexpansion
set "i=0"&set "zero="&set tmp=0
for /l %%i in (0,1,%~2) do (set /a "rand=!random!%%%~1"&if !rand!==0 (set /a tmp=!tmp!+1))
call :length "%~1"
for /l %%i in (2,1,%errorlevel%) do (set "zero=!zero!0")
endlocal&set zero=%zero%&exit /b %tmp%
set scale=10000
echo. 利用随机数获取1/x的值.
echo. 下限: 1 (不包含), 上限: 32767 (不包含)
:loop
set /p input=请输入x :
call :test %input% %scale%
echo. 1/%input% 约等于 0.%zero%%errorlevel%
echo.&pause>nul&goto :loop
:length
setlocal enabledelayedexpansion
set "a=%~1"&set n=0
:length_loop
if "!a!"=="" (endlocal&exit /b %n%)
set "a=!a:~0,-1!"&set /a "n=!n!+1"
goto :length_loop
:test
setlocal enabledelayedexpansion
set "i=0"&set "zero="&set tmp=0
for /l %%i in (0,1,%~2) do (set /a "rand=!random!%%%~1"&if !rand!==0 (set /a tmp=!tmp!+1))
call :length "%~1"
for /l %%i in (2,1,%errorlevel%) do (set "zero=!zero!0")
endlocal&set zero=%zero%&exit /b %tmp%