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-08-09 14:51
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Bonus question for beginners - random value taking View 2,067 Replies 24
Floor 16 Posted 2008-08-25 14:31 ·  中国 广西 百色 中移铁通
银牌会员
★★★
永远的菜鸟
Credits 1,335
Posts 574
Joined 2007-11-27 12:50
18-year member
UID 103929
Gender Male
From 广西
Status Offline

@echo off
Setlocal Enabledelayedexpansion
set "var1=a b c d e f g h i j k l m n o p q r s t u v w x y z"
for /l %%a in (1 1 26) do (set "var2=!var2!%%a ")
for %%a in (%var1%) do (set /a "nn+=1"&set "!nn!=%%a"&set "%%a=!nn!")
echo %var1%&echo %var2%&echo.
:lp
set /a "bt=%random%%%26+1","aa=%random%%%2+1"
for /f "tokens=%bt%" %%a in ("!var%aa%!") do (
if defined .%%a goto :lp
if defined .!%%a! goto :lp
set "ech=!ech! %%a"
set ".%%a=ok"&set ".!%%a!=ok"
)
set /a mm+=1
if %mm% lss 10 goto :lp
echo %ech%
for %%a in (%var1%) do (set ".%%a="&set ".!%%a!=")
set "mm=0"&set "ech="
pause>nul&goto :lp


[ Last edited by moniuming on 2008-8-25 at 02:33 PM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
bat-zw +8 2008-08-25 16:46
Floor 17 Posted 2008-08-25 16:35 ·  中国 广东 江门 电信
中级用户
★★
Credits 338
Posts 175
Joined 2007-10-21 15:30
18-year member
UID 100351
Gender Male
Status Offline
Thanks to the senior for the guidance...
Will go to study...
Floor 18 Posted 2008-08-25 16:48 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
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.
批处理之家新域名:www.bathome.net
Floor 19 Posted 2008-08-25 16:58 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
The following is my solution:

@echo off&setlocal enabledelayedexpansion
for %%i in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
set /a n+=1,a=!random!%%2
if !a! equ 0 (
set "_!random!!random!!random!=!n!"
) else (
set "_!random!!random!!random!=%%i"
)
)
for /f "tokens=2 delims==" %%i in ('set _') do (
set /a n-=1
if !n! equ 15 pause>nul&goto :eof
set /p=%%i <nul
)
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
moniuming +8 2008-08-26 18:54
批处理之家新域名:www.bathome.net
Floor 20 Posted 2008-08-25 20:03 ·  中国 广东 江门 电信
中级用户
★★
Credits 338
Posts 175
Joined 2007-10-21 15:30
18-year member
UID 100351
Gender Male
Status Offline
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 ]
Floor 21 Posted 2008-08-25 23:16 ·  中国 云南 昆明 电信
中级用户
★★
Credits 220
Posts 99
Joined 2008-06-26 15:12
18-year member
UID 120818
Gender Male
Status Offline
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"
Floor 22 Posted 2008-08-26 13:08 ·  中国 四川 泸州 电信
初级用户
Credits 30
Posts 18
Joined 2008-05-11 01:43
18-year member
UID 118377
Gender Male
Status Offline
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
Floor 23 Posted 2008-08-26 13:24 ·  中国 广东 深圳 电信
初级用户
Credits 59
Posts 28
Joined 2007-03-23 05:18
19-year member
UID 82622
Gender Male
Status Offline
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.
Floor 24 Posted 2008-08-26 14:34 ·  中国 四川 泸州 电信
初级用户
Credits 30
Posts 18
Joined 2008-05-11 01:43
18-year member
UID 118377
Gender Male
Status Offline
@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
Floor 25 Posted 2008-08-26 16:13 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
&&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.
批处理之家新域名:www.bathome.net
Forum Jump: