中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-29 08:04
47,816 topics / 349,916 posts / today 0 new / 48,265 members
DOS批处理 & 脚本技术(批处理室) » How to generate a random number with batch processing
Printable Version  6,579 / 19
Floor1 cheng059830 Posted 2005-08-16 22:39
初级用户 Posts 15 Credits 46
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 ]
Floor2 无奈何 Posted 2005-08-16 23:53
荣誉版主 Posts 356 Credits 1,338
Extended variable %random% available under NT environment.
Type in command line: echo %random% and have a look.
For detailed information, type in: set /?
Floor3 cheng059830 Posted 2005-08-17 16:37
初级用户 Posts 15 Credits 46
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?
Floor4 chenhui530 Posted 2005-08-17 17:49
高级用户 Posts 273 Credits 772
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
Floor5 JonePeng Posted 2005-08-17 21:41
金牌会员 Posts 1,883 Credits 4,562 From 广东广州
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 ]
Floor6 无奈何 Posted 2005-08-18 11:42
荣誉版主 Posts 356 Credits 1,338
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%
Floor7 cheng059830 Posted 2005-08-18 17:37
初级用户 Posts 15 Credits 46
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.
Floor8 cheng059830 Posted 2005-08-18 18:18
初级用户 Posts 15 Credits 46
I have understood the meaning of "%random:~-1%". Thank you.
Floor9 willsort Posted 2005-08-18 18:32
元老会员 Posts 1,512 Credits 4,432
Re 无奈何:

The code you provided can be simpler:


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 ]
Floor10 无奈何 Posted 2005-08-19 00:24
荣誉版主 Posts 356 Credits 1,338
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 /?
Floor11 无奈何 Posted 2005-08-19 00:29
荣誉版主 Posts 356 Credits 1,338
Originally posted by willsort at 2005-8-18 18:32:
Re 无奈何:

The code you provided can be simpler:


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.
Floor12 无奈何 Posted 2005-08-19 00:42
荣誉版主 Posts 356 Credits 1,338
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?
Floor13 willsort Posted 2005-08-19 16:06
元老会员 Posts 1,512 Credits 4,432
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.

Floor14 无奈何 Posted 2005-08-19 23:30
荣誉版主 Posts 356 Credits 1,338
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.

Floor15 willsort Posted 2005-08-21 18:20
元老会员 Posts 1,512 Credits 4,432
Re 无奈何(14楼):

既然如上,索性如下:



[ Last edited by willsort on 2005-8-21 at 18:23 ]
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023