|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 16 楼』:
使用 LLM 解释/回答一下
发现了一个不可思议的现象:如果在6楼代码首尾的合适位置添加跳转语句和接收标签,从而使代码循环执行的话,则执行到第20次之后,就会只出现前4个数值,然后程序开始在 :loop 和 if %count% lss 5 goto loop 这个循环体内疯狂运行,第5个数值一直不能出现。嘿嘿,怪事。
————————————————————————————————
原因分析请看41楼,解决问题的代码请看42楼。
Last edited by namejm on 2007-1-6 at 04:58 PM ]
An incredible phenomenon was discovered: If jump statements and receiving tags are added at appropriate positions at the beginning and end of the code on the 6th floor to make the code execute in a loop, then after executing 20 times, only the first 4 values will appear, and then the program starts to run wildly within the loop body of :loop and if %count% lss 5 goto loop. The 5th value can never appear. Hey, strange thing.
————————————————————————————————
For the cause analysis, please see floor 41, and for the code to solve the problem, please see floor 42.
Last edited by namejm on 2007-1-6 at 04:58 PM ]
|

尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2007-1-5 07:07 |
|
|
tghksj
社区乞丐
此图片另存后死机
积分 -49
发帖 90
注册 2006-12-2
状态 离线
|
『第 17 楼』:
使用 LLM 解释/回答一下
没什么好奇怪的,你没加 setlocal
13楼的代码已经可以解决问题了只是还有
请问 namejm 老师
%random%%%99
是什么意思啊???
Last edited by tghksj on 2007-1-4 at 06:26 PM ]
There's nothing strange about it, you didn't add setlocal. The code on the 13th floor can already solve the problem but there's still... Excuse me, Teacher namejm, what does %random%%%99 mean???
[[i] Last edited by tghksj on 2007-1-4 at 06:26 PM [/i]]
|

我的网络笔记本.[color=Red]非联系本人请勿访问![/color]http://w.vicp.net[img]http://zhenlove.com.cn/cndos/fileup/files/QM2.jpg[/img] |
|
2007-1-5 07:18 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 18 楼』:
使用 LLM 解释/回答一下
表示用一个随机数除以99,然后取余数,也就是数学上所说的模运算。
13楼的代码确实不会出现重复值,但是,要出现一位数的几率就太小了,简直是千年等一回呢^_^。
It means dividing a random number by 99 and taking the remainder, which is the so-called modulo operation in mathematics.
The code on the 13th floor indeed won't have repeated values, but the probability of getting a single-digit number is too small, it's like waiting for a millennium ^_^.
|

尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2007-1-5 07:27 |
|
|
youxi01
高级用户
   
积分 846
发帖 247
注册 2006-10-27 来自 湖南==》广东
状态 离线
|
『第 19 楼』:
使用 LLM 解释/回答一下
Originally posted by namejm at 2007-1-5 02:16 AM:
看了4楼的代码,推测 youxi01 的本意是想显示5个 1-100 之间(不包含100)不同的随机数,但是代码是有问题的:%a% 是 1-5 的数,用 if not defined 来判断 n ...
我要的效果就是要它没有定义过,要是定义过了,那不就重复了嘛!
The effect I want is that it hasn't been defined. If it had been defined, wouldn't that be a repeat?
|
|
2007-1-5 07:35 |
|
|
tghksj
社区乞丐
此图片另存后死机
积分 -49
发帖 90
注册 2006-12-2
状态 离线
|
『第 20 楼』:
使用 LLM 解释/回答一下
谢谢老师的说明.
13楼只有当 %random% LSS 10 的时候才会有 1位数 呵呵 几率确实很小的.
6楼的代码确实不知道什么原因......
多次执行之后就不能使用了????!!
%count% 永远==0了...????奇怪哦
清空count 后 setlocal 也不管用了...纳闷....
等高人解释一下了......
Last edited by tghksj on 2007-1-4 at 06:39 PM ]
Thanks for the teacher's explanation.
Only when %random% LSS 10 on the 13th floor will there be a single digit. Hehe, the probability is indeed very small.
I really don't know why the code on the 6th floor......
After executing multiple times, it can't be used anymore????!!
%count% is always ==0...???? Strange oh
Emptying count and then setlocal doesn't work either... Confused....
Waiting for an expert to explain......
[[i] Last edited by tghksj on 2007-1-4 at 06:39 PM [/i]]
|

我的网络笔记本.[color=Red]非联系本人请勿访问![/color]http://w.vicp.net[img]http://zhenlove.com.cn/cndos/fileup/files/QM2.jpg[/img] |
|
2007-1-5 07:36 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 21 楼』:
使用 LLM 解释/回答一下
Originally posted by youxi01 at 2007-1-4 18:35:
我要的效果就是要它没有定义过,要是定义过了,那不就重复了嘛!
实际上,if not defined !_num%a%! 这一句始终为真,也就是说,这一部分语句有和没有的效果是一样的,并不能控制那个变量是否已经被定义过,你添加跳转语句来循环执行,多次执行之后,就会看到有重复的数值出现了。可能你还不太明白我为什么说那一句始终为真,索性再多说几句吧:你在上一句用了 set /a _num!a!=%random:~-2% 来定义变量 _num!a!,下一句你用 if not defined !_num%a%! 来判断的是 变量_num!a! 的值(而非变量本身) 是否被定义。
另外,你的代码并没有处理 08 和 09 ,会出错的。
Last edited by namejm on 2007-1-4 at 07:16 PM ]
Originally posted by youxi01 at 2007-1-4 18:35:
The effect I want is that it hasn't been defined. If it has been defined, it will be repeated!
Actually, the sentence if not defined !_num%a%! is always true, that is to say, the effect of this part of the statement is the same with or without it, and it cannot control whether that variable has been defined. You add jump statements to execute in a loop, and after multiple executions, you will see repeated values. Maybe you don't quite understand why I say that sentence is always true, so I'll say a few more words: You used set /a _num!a!=%random:~-2% in the previous sentence to define the variable _num!a!, and in the next sentence, you use if not defined !_num%a%! to judge whether the value of variable _num!a! (not the variable itself) has been defined.
In addition, your code does not handle 08 and 09, which will cause errors.
Last edited by namejm on 2007-1-4 at 07:16 PM ]
|

尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2007-1-5 07:57 |
|
|
balinger
中级用户
  
积分 356
发帖 115
注册 2004-7-27
状态 离线
|
『第 22 楼』:
使用 LLM 解释/回答一下
复杂的事交给电脑去做。我来个简单的。
@echo off
:loop
set s1=%RANDOM:~-2%
set s2=%RANDOM:~-2%
set s3=%RANDOM:~-2%
set s4=%RANDOM:~-2%
set s5=%RANDOM:~-2%
set /a h=s1*s2*s3*s4*s5*(s1-s2)*(s1-s3)*(s1-s4)*(s1-s5)*(s2-s3)*(s2-s4)*(s2-s5)*(s3-s4)*(s3-s5)*(s4-s5)
if h equ 0 goto :loop
echo %s1% %s2% %s3% %s4% %s5%
pause
goto :loop
Leave complex tasks to the computer. I'll do something simple.
@echo off
:loop
set s1=%RANDOM:~-2%
set s2=%RANDOM:~-2%
set s3=%RANDOM:~-2%
set s4=%RANDOM:~-2%
set s5=%RANDOM:~-2%
set /a h=s1*s2*s3*s4*s5*(s1-s2)*(s1-s3)*(s1-s4)*(s1-s5)*(s2-s3)*(s2-s4)*(s2-s5)*(s3-s4)*(s3-s5)*(s4-s5)
if h equ 0 goto :loop
echo %s1% %s2% %s3% %s4% %s5%
pause
goto :loop
|
|
2007-1-5 08:02 |
|
|
tao0610
高级用户
    朦胧的世界
积分 579
发帖 218
注册 2006-10-24
状态 离线
|
『第 23 楼』:
使用 LLM 解释/回答一下
13楼的代码可以简化一下.
@ECHO OFF
setlocal ENABLEDELAYEDEXPANSION
SET T=1
:PR
FOR /L %%A IN (%T%,1,100) DO (
SET/a NO%%A=!random!%%100+1
SET /A H=%%A-1
FOR /L %%B IN (!H!,-1,1) DO (
IF !NO%%A!==!NO%%B! (SET T=%%A&GOTO PR))
echo !NO%%A!>>222.txt)
pause
批处理对CALL和FOR的嵌套执行效率并不高,能不用最好不用.
@echo off&setlocal enabledelayedexpansion
set t=1
:PR
for /l %%a in (%t% 1 100) do (
set/a a%%a=!random!%%100+1
if not defined !a%%a! (echo !a%%a!>>123.txt) else set t=%%a&goto PR
set !a%%a!=flag
)
pause
The code on floor 13 can be simplified.
@ECHO OFF
setlocal ENABLEDELAYEDEXPANSION
SET T=1
:PR
FOR /L %%A IN (%T%,1,100) DO (
SET/a NO%%A=!random!%%100+1
SET /A H=%%A-1
FOR /L %%B IN (!H!,-1,1) DO (
IF !NO%%A!==!NO%%B! (SET T=%%A&GOTO PR))
echo !NO%%A!>>222.txt)
pause
Batch processing has low execution efficiency for nested execution of CALL and FOR, so it is best not to use them if possible.
@echo off&setlocal enabledelayedexpansion
set t=1
:PR
for /l %%a in (%t% 1 100) do (
set/a a%%a=!random!%%100+1
if not defined !a%%a! (echo !a%%a!>>123.txt) else set t=%%a&goto PR
set !a%%a!=flag
)
pause
|

认识自己,降伏自己,改变自己,才能改变别人! |
|
2007-1-5 08:02 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 24 楼』:
使用 LLM 解释/回答一下
22楼通过几个数字相减是否为0来判断数字是否重复,确实是个思路,只是仍然没有处理好把字符串数值化的细节,导致截取到形如 02、03 的字符串(而非数值)。
The idea of building the 22nd floor by subtracting several numbers to see if the result is 0 to judge whether the numbers are repeated is indeed a thought, but the details of converting the string to a numerical value are still not handled well, resulting in intercepting strings in the form of "02", "03" (rather than numerical values).
|

尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2007-1-5 08:08 |
|
|
youxi01
高级用户
   
积分 846
发帖 247
注册 2006-10-27 来自 湖南==》广东
状态 离线
|
『第 25 楼』:
使用 LLM 解释/回答一下
Originally posted by namejm at 2007-1-5 07:57 AM:
实际上,if not defined !_num%a%! 这一句始终为真,也就是说,这一部分语句有和没有的效果是一样的,并不能控制那个变量是否已经被定义过,你淠...
明白了,还真的是笨,哈哈,不好意思!
Originally posted by namejm at 2007-1-5 07:57 AM:
Actually, the statement "if not defined !_num%a%!" is always true, which means that the effect of this part of the statement is the same with or without it, and it cannot control whether the variable has been defined or not, you...
Got it, really stupid, haha, sorry!
|
|
2007-1-5 08:21 |
|
|
balinger
中级用户
  
积分 356
发帖 115
注册 2004-7-27
状态 离线
|
『第 26 楼』:
使用 LLM 解释/回答一下
Originally posted by namejm at 2007-1-5 08:08 AM:
22楼通过几个数字相减是否为0来判断数字是否重复,确实是个思路,只是仍然没有处理好把字符串数值化的细节,导致截取到形如 02、03 的字符串 ...
多谢指教,修改如下:
@echo off
:loop
set s1=%RANDOM:~-2%
set /a s1=s1*1
set s2=%RANDOM:~-2%
set /a s2=s2*1
set s3=%RANDOM:~-2%
set /a s3=s3*1
set s4=%RANDOM:~-2%
set /a s4=s4*1
set s5=%RANDOM:~-2%
set /a s5=s5*1
set /a h=s1*s2*s3*s4*s5*(s1-s2)*(s1-s3)*(s1-s4)*(s1-s5)*(s2-s3)*(s2-s4)*(s2-s5)*(s3-s4)*(s3-s5)*(s4-s5)
if %h% EQU 0 goto :loop
echo %s1% %s2% %s3% %s4% %s5%
pause
goto :loop
Originally posted by namejm at 2007-1-5 08:08 AM:
The 22nd floor uses the subtraction of several numbers to determine whether it is 0 to judge whether the numbers are repeated. It is indeed an idea, but the details of converting the string to a numerical value are still not handled well, resulting in intercepting strings in the form of 02, 03...
Thank you for the advice. The modification is as follows:
@echo off
:loop
set s1=%RANDOM:~-2%
set /a s1=s1*1
set s2=%RANDOM:~-2%
set /a s2=s2*1
set s3=%RANDOM:~-2%
set /a s3=s3*1
set s4=%RANDOM:~-2%
set /a s4=s4*1
set s5=%RANDOM:~-2%
set /a s5=s5*1
set /a h=s1*s2*s3*s4*s5*(s1-s2)*(s1-s3)*(s1-s4)*(s1-s5)*(s2-s3)*(s2-s4)*(s2-s5)*(s3-s4)*(s3-s5)*(s4-s5)
if %h% EQU 0 goto :loop
echo %s1% %s2% %s3% %s4% %s5%
pause
goto :loop
|
|
2007-1-5 09:12 |
|
|
26933062
银牌会员
    
积分 2268
发帖 879
注册 2006-12-19
状态 离线
|
『第 27 楼』:
使用 LLM 解释/回答一下
请问斑主,你6楼的代码中红字部分是什么意识?
@echo off
set count=0
:loop
set /a num=%random%%%99+1
if not defined num%num% (
set num%num%=%num%
set /a count+=1
call echo %%num%%
)
if %count% lss 5 goto loop
pause>nul
May I ask the moderator, what does the red part in the code on floor 6 mean?
@echo off
set count=0
:loop
set /a num=%random%%%99+1
if not defined num%num% (
set num%num%=%num%
set /a count+=1
call echo %%num%%
)
if %count% lss 5 goto loop
pause>nul
|
|
2007-1-5 09:51 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 28 楼』:
使用 LLM 解释/回答一下
楼主你终于来了啊,可把大家想坏了。盼星星盼月亮,终于把你盼到了,总算在顶楼把意思表达清楚了,看来大家的猜测还不算离谱,呵呵。
玩笑过后,言归正传。defined 的词义是定义的意思,在批处理中,用 if defined 变量名 这样的语句来检测某个变量是否已经被定义过(也就是有没有被赋值)。既然你要在1-100(包括1和100)这个范围内显示随机数,把我那个代码里的99改为100就可以了。
The owner, you finally came. Everyone has been missing you so much. We've been looking forward to you like stars and moons, and finally you're here. It seems that everyone's guesses aren't too far off, heh heh.
After the joke, let's get down to business. The meaning of "defined" is "defined". In batch processing, use a statement like if defined variable name to check whether a certain variable has been defined (that is, whether it has been assigned a value). Since you want to display a random number in the range of 1-100 (including 1 and 100), you can change 99 to 100 in my code.
|

尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2007-1-5 10:04 |
|
|
26933062
银牌会员
    
积分 2268
发帖 879
注册 2006-12-19
状态 离线
|
『第 29 楼』:
使用 LLM 解释/回答一下
谢谢版主的解答,不过你好像又在21楼推翻了自己的方法,(21楼的解释我没看的太懂)那么你6楼的方法到底行不行呢?我试过好几次了,好像还行.
Thanks for the moderator's explanation, but it seems you overturned your own method in post 21. (I didn't understand the explanation in post 21 very well.) Then, does the method in your post 6 work? I've tried it several times, and it seems to be okay.
|
|
2007-1-5 10:12 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 30 楼』:
使用 LLM 解释/回答一下
4楼的思路是正确的,我在21楼只是指出了4楼代码的错误之处而已,而6楼的代码修正了那些错误,并没有推翻我的方法。
另外,我在16楼描述了一个奇怪的现象,可能是call语句的特殊之处,也可能是CMD的一个bug,我现在都还没搞明白是怎么回事,希望能引起你的重视。
——————————————————————————————————————
原因已经找到,请看41楼的分析,然后再使用42楼的代码。
Last edited by namejm on 2007-1-6 at 05:08 PM ]
The idea on the 4th floor is correct. I just pointed out the errors in the code on the 21st floor, and the code on the 6th floor corrected those errors without overthrowing my method.
In addition, I described a strange phenomenon on the 16th floor. It might be the particularity of the call statement or a bug in CMD. I still don't understand what's going on now, and I hope it can attract your attention.
——————————————————————————————————————
The reason has been found. Please refer to the analysis on the 41st floor, and then use the code on the 42nd floor.
Last edited by namejm on 2007-1-6 at 05:08 PM ]
|

尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2007-1-5 10:27 |
|