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-06-22 13:48
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Randomly display 5 different numbers View 6,591 Replies 57
Floor 16 Posted 2007-01-05 07:07 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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没商量。
考虑问题复杂化,解决问题简洁化。
Floor 17 Posted 2007-01-05 07:18 ·  中国 山东 济南 电信
社区乞丐
★★
此图片另存后死机
Credits -49
Posts 90
Joined 2006-12-02 13:00
19-year member
UID 72412
Gender Male
Status Offline
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???

[ Last edited by tghksj on 2007-1-4 at 06:26 PM ]
我的网络笔记本.非联系本人请勿访问!http://w.vicp.net
Floor 18 Posted 2007-01-05 07:27 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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没商量。
考虑问题复杂化,解决问题简洁化。
Floor 19 Posted 2007-01-05 07:35 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
The effect I want is that it hasn't been defined. If it had been defined, wouldn't that be a repeat?
Floor 20 Posted 2007-01-05 07:36 ·  中国 山东 济南 电信
社区乞丐
★★
此图片另存后死机
Credits -49
Posts 90
Joined 2006-12-02 13:00
19-year member
UID 72412
Gender Male
Status Offline
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......

[ Last edited by tghksj on 2007-1-4 at 06:39 PM ]
我的网络笔记本.非联系本人请勿访问!http://w.vicp.net
Floor 21 Posted 2007-01-05 07:57 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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没商量。
考虑问题复杂化,解决问题简洁化。
Floor 22 Posted 2007-01-05 08:02 ·  中国 福建 福州 连江县 电信
中级用户
★★
Credits 356
Posts 115
Joined 2004-07-27 00:00
21-year member
UID 29114
Gender Male
Status Offline
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
Floor 23 Posted 2007-01-05 08:02 ·  中国 北京 朝阳区 联通
高级用户
★★
朦胧的世界
Credits 579
Posts 218
Joined 2006-10-24 04:29
19-year member
UID 67972
Status Offline
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

认识自己,降伏自己,改变自己
,才能改变别人!
Floor 24 Posted 2007-01-05 08:08 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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没商量。
考虑问题复杂化,解决问题简洁化。
Floor 25 Posted 2007-01-05 08:21 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
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!
Floor 26 Posted 2007-01-05 09:12 ·  中国 福建 福州 连江县 电信
中级用户
★★
Credits 356
Posts 115
Joined 2004-07-27 00:00
21-year member
UID 29114
Gender Male
Status Offline
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
Floor 27 Posted 2007-01-05 09:51 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
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
Floor 28 Posted 2007-01-05 10:04 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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没商量。
考虑问题复杂化,解决问题简洁化。
Floor 29 Posted 2007-01-05 10:12 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
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.
Floor 30 Posted 2007-01-05 10:27 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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没商量。
考虑问题复杂化,解决问题简洁化。
Forum Jump: