|
cheng059830
初级用户
 
积分 46
发帖 15
注册 2005-8-13
状态 离线
|
『楼 主』:
(已结)如何用批处理生成一个随机数
使用 LLM 解释/回答一下
我想用批处理生成一个随机数.有方法可以实现.
---------- Edited by willsort ----------
有以下方案:
1、直接引用环境变量 %random% (2楼),
可用程序控制其生成位数(6,9,13楼)可在WinNT系列环境下
2、VBS脚本生成(4楼),可在Windows下使用
3、strings+LMOD(5楼),可在Windows和DOS下使用
4、纯批处理(9楼),可在Windows或DOS下使用
---------- Edited by willsort ----------
Last edited by willsort on 2005-8-19 at 16:14 ]
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 ]
|
|
2005-8-16 22:39 |
|
|
无奈何
荣誉版主
      
积分 1338
发帖 356
注册 2005-7-15
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
nt环境下可用扩展变量%random%
命令行下键入:echo %random% 看看。
详细信息键入:set /?
Extended variable %random% available under NT environment.
Type in command line: echo %random% and have a look.
For detailed information, type in: set /?
|
|
2005-8-16 23:53 |
|
|
cheng059830
初级用户
 
积分 46
发帖 15
注册 2005-8-13
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
能不能详细说一下例子.
如果我需要生成1到100之间的随机数,应该怎样写.
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?
|
|
2005-8-17 16:37 |
|
|
chenhui530
高级用户
   
积分 772
发帖 273
注册 2004-10-23
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
DOS下我不知道
但是VBS倒是很简单
Dim MyValue, Response
Randomize '初始化随机数生成器。
Do Until Response = vbNo
MyValue = Int((6 * Rnd) + 1) ' 产生 1 到 6 之间的随机数。
MsgBox MyValue
Response = MsgBox ("Roll again? ", vbYesNo)
Loop
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/ |
|
2005-8-17 17:49 |
|
|
JonePeng
金牌会员
      D◎$ Fαп
积分 4562
发帖 1883
注册 2004-1-19 来自 广东广州
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
%random%变量只适用于NT环境下,这时如果用Strings再配合LMOD则可以生成一个随机数。%random%的特点是随机产生一个0~32768之间的整数,因此用 strings 命令将%random%和327相除,就可以得到0~100的整数了。
...
strings div %random%,327 | Lmod set xyz= >tmp.bat
call tmp.bat >nul
del tmp.bat
...
这样就可以产生一个随机数到%xyz%,可以随时调用了。
可惜该方法不能在纯DOS和Win9x下通过。
Last edited by JonePeng on 2005-8-17 at 23:19 ]
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!≡≡≡≡====----
|
|
2005-8-17 21:41 |
|
|
无奈何
荣誉版主
      
积分 1338
发帖 356
注册 2005-7-15
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
Originally posted by cheng059830 at 2005-8-17 16:37:
能不能详细说一下例子.
如果我需要生成1到100之间的随机数,应该怎样写.
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%
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%
|
|
2005-8-18 11:42 |
|
|
cheng059830
初级用户
 
积分 46
发帖 15
注册 2005-8-13
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
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%
能不能说明一下"!random:~-1!"是什么意思.
谢谢.
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.
|
|
2005-8-18 17:37 |
|
|
cheng059830
初级用户
 
积分 46
发帖 15
注册 2005-8-13
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
已经明白"%random:~-1%"这个意思了.谢谢.
I have understood the meaning of "%random:~-1%". Thank you.
|
|
2005-8-18 18:18 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
Re 无奈何:
你给出的代码可以更简单些:
set x=%random:~-3%
echo Random number: %x%
另外,Win9x和DOS下获取随机数到环境变量可以参考 {8905}批处理编程的异类 3楼的代码。
Last edited by willsort on 2005-8-18 at 18:34 ]
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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2005-8-18 18:32 |
|
|
无奈何
荣誉版主
      
积分 1338
发帖 356
注册 2005-7-15
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
Originally posted by cheng059830 at 2005-8-18 17:37:
能不能说明一下"!random:~-1!"是什么意思.
谢谢.
"!random:~-1!" 两个!!引起来的是延迟环境变量,详细见 set /?
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 /?
|
|
2005-8-19 00:24 |
|
|
无奈何
荣誉版主
      
积分 1338
发帖 356
注册 2005-7-15
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
Originally posted by willsort at 2005-8-18 18:32:
Re 无奈何:
你给出的代码可以更简单些:
set x=%random:~-3%
echo Random number: %x%
另外,Win9x和DOS下获取随机数到环境变量可以参考
版主就是版主利害。不过我不知道 %random%获得的随机数小于3位时会出现什么情况?我的确实笨了些。
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.
|
|
2005-8-19 00:29 |
|
|
无奈何
荣誉版主
      
积分 1338
发帖 356
注册 2005-7-15
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
顺便问一下:
我原先写的是这样:
setlocal ENABLEDELAYEDEXPANSION
for /l %%i in (1,1,3) do (
echo %random% >nul
set x=!random:~-1!
)
echo %x%
但只返回一位数,为什么用了延迟环境变量仍还不行,谁能给个解答?
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?
|
|
2005-8-19 00:42 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
Re 无奈何(12楼):
因为你在循环中没有依次将 %random% 的最后一位除去,所以每次引用的都是 %random% 的相同的最后一位;同时,因为你多次引用了 %random%,而每次被引用的 %random% 都是不同的,所以不会组合得到最初的随机数的最后三位数。
按照你的思路,我写了以下的代码。当然,程序的复杂是我起初想不到的,可见良好的算法和技巧可以省却很多编程上的无用功。目前,下面的代码处理三位数以下的随机数仍然是有错的,这源于 set 环境延迟对空变量的识别机制与我们想象的不同。
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%
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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2005-8-19 16:06 |
|
|
无奈何
荣誉版主
      
积分 1338
发帖 356
注册 2005-7-15
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
Re willsort版主 『第 13 楼』:
谢谢版主的热心解答,我的原意是想累计获取3次不同的 %random%的最后一位,看了你的程序我才意识到我犯了逻辑错误,不知道我当初怎么想的。<img src="images/smilies/face-raspberry.png" align="absmiddle" border="0">
你的代码处理三位数以下确实有错,我试了300次才总算找到一个:
Old random number: 87
New random number: ~-187
换了个思路重写了一下,应该没有问题了。
setlocal ENABLEDELAYEDEXPANSION
for /l %%i in (1,1,3) do (
call :loop
)
echo %x%
goto :EOF
:loop
set x=!x!%random:~-1%
goto :EOF
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. :P
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
|
|
2005-8-19 23:30 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
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 ]
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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2005-8-21 18:20 |
|