China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-07-14 23:27
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to generate a random number with batch processing View 6,307 Replies 19
Original Poster Posted 2005-08-16 22:39 ·  中国 广西 梧州 电信
初级用户
Credits 46
Posts 15
Joined 2005-08-13 12:11
20-year member
UID 41615
Status Offline
I want to generate a random number with batch processing. There are methods to achieve it.

---------- Edited by willsort ----------
There are the following solutions:
1. Directly reference the environment variable %random% (floor 2),
The number of digits can be controlled by the program (6, 9, floor 13), which can be in the WinNT series environment
2. VBS script generation (floor 4), which can be used under Windows
3. strings + LMOD (floor 5), which can be used under Windows and DOS
4. Pure batch processing (floor 9), which can be used under Windows or DOS
---------- Edited by willsort ----------


[ Last edited by willsort on 2005-8-19 at 16:14 ]
Floor 2 Posted 2005-08-16 23:53 ·  中国 辽宁 锦州 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
20-year member
UID 40733
Gender Male
Status Offline
Extended variable %random% available under NT environment.
Type in command line: echo %random% and have a look.
For detailed information, type in: set /?
Floor 3 Posted 2005-08-17 16:37 ·  中国 广西 梧州 电信
初级用户
Credits 46
Posts 15
Joined 2005-08-13 12:11
20-year member
UID 41615
Status Offline
Can you give a detailed explanation of the example?
If I need to generate a random number between 1 and 100, how should I write it?
Floor 4 Posted 2005-08-17 17:49 ·  中国 重庆 沙坪坝区 电信
高级用户
★★
Credits 772
Posts 273
Joined 2004-10-23 00:00
21-year member
UID 32861
Gender Male
Status Offline
In DOS, I don't know, but VBS is quite simple.
Dim MyValue, Response
Randomize 'Initialize the random number generator.
Do Until Response = vbNo
MyValue = Int((6 * Rnd) + 1) 'Generate a random number between 1 and 6.
MsgBox MyValue
Response = MsgBox ("Roll again? ", vbYesNo)
Loop
http://www.msfans.net/bbs/
Floor 5 Posted 2005-08-17 21:41 ·  中国 广东 广州 白云区 电信
金牌会员
★★★★
D◎$ Fαп
Credits 4,562
Posts 1,883
Joined 2004-01-19 00:00
22-year member
UID 15812
Gender Male
From 广东广州
Status Offline
The %random% variable is only applicable in the NT environment. At this time, if you use Strings and then cooperate with LMOD, you can generate a random number. The characteristic of %random% is that it randomly generates an integer between 0 and 32768. Therefore, by using the strings command to divide %random% by 327, you can get an integer between 0 and 100.

...
strings div %random%,327 | Lmod set xyz= >tmp.bat
call tmp.bat >nul
del tmp.bat
...

In this way, a random number can be generated to %xyz%, which can be called at any time.
Unfortunately, this method cannot pass in pure DOS and Win9x.

[ Last edited by JonePeng on 2005-8-17 at 23:19 ]
----====≡≡≡≡ 我的至爱,永远是MSDOS!≡≡≡≡====----
Floor 6 Posted 2005-08-18 11:42 ·  中国 辽宁 锦州 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
20-year member
UID 40733
Gender Male
Status Offline
Originally posted by cheng059830 at 2005-8-17 16:37:
Can you give a detailed example.
If I need to generate a random number between 1 and 100, how should I write it.


setlocal ENABLEDELAYEDEXPANSION
for /l %%i in (1,1,3) do (
echo %random% >nul
set x%%i=!random:~-1!
)
set x=%x1%%x2%%x3%
echo %x%
Floor 7 Posted 2005-08-18 17:37 ·  中国 广西 梧州 联通
初级用户
Credits 46
Posts 15
Joined 2005-08-13 12:11
20-year member
UID 41615
Status Offline
Originally posted by 无奈何 at 2005-8-18 11:42:


setlocal ENABLEDELAYEDEXPANSION
for /l %%i in (1,1,3) do (
echo %random% >nul
set x%%i=!random:~-1!
)
set x=%x1%%x2%%x3%
echo %x%




What does "!random:~-1!" mean?
Thanks.
Floor 8 Posted 2005-08-18 18:18 ·  中国 广西 梧州 联通
初级用户
Credits 46
Posts 15
Joined 2005-08-13 12:11
20-year member
UID 41615
Status Offline
I have understood the meaning of "%random:~-1%". Thank you.
Floor 9 Posted 2005-08-18 18:32 ·  中国 山西 运城 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re 无奈何:

The code you provided can be simpler:
set x=%random:~-3%
echo Random number: %x%


Additionally, for obtaining random numbers into environment variables under Win9x and DOS, you can refer to the code on floor 3 of {8905} A Different Kind of Batch Programming.

[ Last edited by willsort on 2005-8-18 at 18:34 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 10 Posted 2005-08-19 00:24 ·  中国 辽宁 锦州 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
20-year member
UID 40733
Gender Male
Status Offline
Originally posted by cheng059830 at 2005-8-18 17:37:




Can you explain what "!random:~-1!" means.
Thanks.

"!random:~-1!" The two !! enclose a delayed expansion environment variable. For details, see set /?
Floor 11 Posted 2005-08-19 00:29 ·  中国 辽宁 锦州 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
20-year member
UID 40733
Gender Male
Status Offline
Originally posted by willsort at 2005-8-18 18:32:
Re 无奈何:

The code you provided can be simpler:
set x=%random:~-3%
echo Random number: %x%


In addition, obtaining random numbers to environment variables under Win9x and DOS can refer to
The moderator is indeed powerful. But I don't know what will happen when the random number obtained by %random% is less than 3 digits? I'm really stupid.
Floor 12 Posted 2005-08-19 00:42 ·  中国 辽宁 锦州 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
20-year member
UID 40733
Gender Male
Status Offline
By the way:
What I originally wrote was like this:
setlocal ENABLEDELAYEDEXPANSION
for /l %%i in (1,1,3) do (
echo %random% >nul
set x=!random:~-1!
)
echo %x%
But it only returns a single digit. Why does it still not work after using delayed environment variables? Can someone give an explanation?
Floor 13 Posted 2005-08-19 16:06 ·  中国 山西 运城 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re 无奈何(12楼):

Because you didn't remove the last digit of %random% one by one in the loop, so each time you reference the same last digit of %random%; also, because you referenced %random% multiple times, and each referenced %random% is different, so you won't combine to get the last three digits of the original random number.

According to your idea, I wrote the following code. Of course, the complexity of the program is what I didn't think of at first. It can be seen that good algorithms and skills can save a lot of useless work in programming. Currently, the following code still has errors when handling random numbers with fewer than three digits, which comes from the different recognition mechanism of the set environment delay for empty variables from what we imagine.


setlocal ENABLEDELAYEDEXPANSION
set y=%random%
set x=
echo Old random number: %y%
for /l %%i in (1,1,3) do (
if "%%i"=="1" (set x=!y:~-1!) else (set x=!y:~-1!!x!)
set y=!y:~0,-1!
)
echo New random number: %x%
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 14 Posted 2005-08-19 23:30 ·  中国 辽宁 锦州 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
20-year member
UID 40733
Gender Male
Status Offline
Re regarding the forum moderator 'Loushang' :

Thanks to the moderator for the enthusiastic explanation. My original intention was to cumulatively obtain the last digit of 3 different %random% values. Only after seeing your program did I realize I made a logical mistake. I don't know what I was thinking back then.

Your code has an error when dealing with numbers less than three digits. I tried 300 times before finally finding one:
Old random number: 87
New random number: ~-187
I changed my approach and rewrote it. It should be fine now.


setlocal ENABLEDELAYEDEXPANSION
for /l %%i in (1,1,3) do (
call :loop
)
echo %x%
goto :EOF

:loop
set x=!x!%random:~-1%
goto :EOF
Floor 15 Posted 2005-08-21 18:20 ·  中国 山西 运城 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re 无奈何(14楼):

既然如上,索性如下:


set x=%random:~-1%%random:~-1%%random:~-1%
echo New random number: %x%


[ Last edited by willsort on 2005-8-21 at 18:23 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Forum Jump: