中国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-11-24 12:14
47,820 topics / 349,938 posts / today 0 new / 48,298 members
DOS批处理 & 脚本技术(批处理室) » Bonus question for beginners - random value taking
Printable Version  2,691 / 24
Floor16 moniuming Posted 2008-08-25 14:31
银牌会员 Posts 574 Credits 1,335 From 广西


[ Last edited by moniuming on 2008-8-25 at 02:33 PM ]
Floor17 BC Posted 2008-08-25 16:35
中级用户 Posts 175 Credits 338
Thanks to the senior for the guidance...
Will go to study...
Floor18 bat-zw Posted 2008-08-25 16:48
金牌会员 Posts 1,276 Credits 3,105
The code on floor 16 uses if judgment and return, so it has efficiency issues to a certain extent, and the code is also more complicated, but it is still given points for encouragement.
Floor19 bat-zw Posted 2008-08-25 16:58
金牌会员 Posts 1,276 Credits 3,105
The following is my solution:
Floor20 BC Posted 2008-08-25 20:03
中级用户 Posts 175 Credits 338
Brother zw's code is really powerful...

Can you write comments?
Can you explain the thinking process?

[ Last edited by BC on 2008-8-25 at 08:07 PM ]
Floor21 23112656 Posted 2008-08-25 23:16
中级用户 Posts 99 Credits 220
Can the moderator brother explain what the meaning of three random numbers connected together is in the following code:
set "_!random!!random!!random!=!n!"
) else (
set "_!random!!random!!random!=%%i"
Floor22 heheguai Posted 2008-08-26 13:08
初级用户 Posts 18 Credits 30
I'm a newbie, but I did it, and the meaning fully meets the requirements of the topic!
@echo off&setlocal enabledelayedexpansion
set char1=abcdefghijklmnopqrstuvwxyz
set char3=1
set num=0
:loop
set /a ran=%random%%%26
set char2=!char1:~%ran%,1!
set /a num1=%ran%+1
if %num% lss 10 (
echo %char3%|find "%char2%" 1>nul
if errorlevel 1 ( set char3=%char3%%char2%&echo %num1%--%char2%&set /a num+=1&goto loop ) else ( goto loop ) )
pause
Floor23 szww Posted 2008-08-26 13:24
初级用户 Posts 28 Credits 59
Please provide the specific code content that needs to be analyzed so that I can help you translate the relevant request and the code for analysis.
Floor24 heheguai Posted 2008-08-26 14:34
初级用户 Posts 18 Credits 30
@echo off&setlocal enabledelayedexpansion
set char1=abcdefghijklmnopqrstuvwxyz
set char3=1
set num=0
:loop
set /a ran=%random%%%26-------This line sets a random number from 0 to 25
set char2=!char1:~%ran%,1!-------This line gets the character corresponding to the random number, for example, if the random number is 5, then it corresponds to 'f' because 0 corresponds to 'a'
set /a num1=%ran%+1------This line is the random number +1, which is mainly used for the subsequent output. 1 corresponds to 'a'
if %num% lss 10 ( -------This line is a loop, looping 10 times
echo %char3%|find "%char2%" 1>nul -----This line judges whether this random number has been generated before
if errorlevel 1 ( set char3=%char3%%char2%&echo %num1%--%char2%&set /a num+=1&goto loop ) else ( goto loop ) ) -------This line is connected to the previous line. If this random number has been generated before, continue to the next loop. If not, increment the loop count by 1, then output the corresponding number and letter, and then continue the loop, so as to remove the situation where the first random generation is 1 and the second random generation is still 1. I think this analysis is all understood
pause
Floor25 bat-zw Posted 2008-08-26 16:13
金牌会员 Posts 1,276 Credits 3,105
&&The attitude of the brother upstairs is still very serious and worthy of praise, but your code does not meet the requirements:
1. What you generated is a corresponding relationship, which is already a condition given in the question in advance and does not need to be generated again;
2. According to your code, only 10 different pure number sequences or pure letter sequences can be generated, while the question requires random value sequences composed of letters and numbers and having no corresponding relationship with each other;
3. The same as other previous codes, using if judgment + return (in your code, it is manifested as not accumulating the count), which has efficiency problems;
4. Using pipes also reduces code efficiency.
Prev  1 2
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023