![]() |
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-10-25 23:51 |
47,815 topics / 349,928 posts / today 0 new / 48,288 members |
| DOS批处理 & 脚本技术(批处理室) » Randomly display 5 different numbers |
| Printable Version 7,763 / 57 |
| Floor16 namejm | Posted 2007-01-05 07:07 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
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 ] |
|
| Floor17 tghksj | Posted 2007-01-05 07:18 |
| 社区乞丐 Posts 90 Credits -49 | |
|
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 ] |
|
| Floor18 namejm | Posted 2007-01-05 07:27 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
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 ^_^. |
|
| Floor19 youxi01 | Posted 2007-01-05 07:35 |
| 高级用户 Posts 247 Credits 846 From 湖南==》广东 | |
|
The effect I want is that it hasn't been defined. If it had been defined, wouldn't that be a repeat?
|
|
| Floor20 tghksj | Posted 2007-01-05 07:36 |
| 社区乞丐 Posts 90 Credits -49 | |
|
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 ] |
|
| Floor21 namejm | Posted 2007-01-05 07:57 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
Originally posted by youxi01 at 2007-1-4 18:35: 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 ] |
|
| Floor22 balinger | Posted 2007-01-05 08:02 |
| 中级用户 Posts 115 Credits 356 | |
|
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 |
|
| Floor23 tao0610 | Posted 2007-01-05 08:02 |
| 高级用户 Posts 218 Credits 579 | |
|
The code on floor 13 can be simplified.
Batch processing has low execution efficiency for nested execution of CALL and FOR, so it is best not to use them if possible. |
|
| Floor24 namejm | Posted 2007-01-05 08:08 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
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).
|
|
| Floor25 youxi01 | Posted 2007-01-05 08:21 |
| 高级用户 Posts 247 Credits 846 From 湖南==》广东 | |
Originally posted by namejm at 2007-1-5 07:57 AM: Got it, really stupid, haha, sorry! |
|
| Floor26 balinger | Posted 2007-01-05 09:12 |
| 中级用户 Posts 115 Credits 356 | |
Originally posted by namejm at 2007-1-5 08:08 AM: 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 |
|
| Floor27 26933062 | Posted 2007-01-05 09:51 |
| 银牌会员 Posts 879 Credits 2,268 | |
|
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 |
|
| Floor28 namejm | Posted 2007-01-05 10:04 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
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. |
|
| Floor29 26933062 | Posted 2007-01-05 10:12 |
| 银牌会员 Posts 879 Credits 2,268 | |
|
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.
|
|
| Floor30 namejm | Posted 2007-01-05 10:27 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
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 ] |
|
| Prev 1 2 3 4 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |