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 15:35
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Randomly display 5 different numbers View 6,598 Replies 57
Original Poster Posted 2007-01-04 16:12 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
Can you display 5-digit numbers randomly from 1 to 100 at once without repetition?
I've thought about it for a long time and searched most of the posts in the forum, but it seems there are no posts that display 5-digit numbers at once, and the key is that there should be no repetition.


Sorry, I'm sorry everyone. It seems my expression ability is really problematic.
My purpose is to display random numbers from 1 to 100, display 5 at a time, and the 5 numbers displayed should not be repeated. Can everyone understand this?


First of all, thank you all for your wonderful answers. Because my batch processing level is really too low, I can't understand some advanced answers well, so please forgive me if there are any wrong evaluations.....................
1. The answer on the 6th floor basically meets the requirements, but there is an occasional dead loop phenomenon when used multiple times. I don't know the reason.
2. The one on the 13th floor has no repetition phenomenon, but the first digit is always the same every time it runs, and I always feel a little inappropriate.
3. The one on the 26th floor can also be used, but it feels too rigid. (I'm sorry, maybe the wording is inappropriate, but I can't think of a more euphemistic word, please forgive me!) For example, if I want to display 6, 7, 8, 9... digits, it's too troublesome. In fact, my original intention is to first require entering the maximum value of the random number, and then entering the number of digits to display.
4. Personally, I feel that (in terms of thinking) the one on the 32nd floor should be the most ideal, but unfortunately I can't understand it, and the brother on the 32nd floor didn't write the code as required. (Displayed 100 at once).
5. I think the code on the 33rd floor should be the best. Judging from the code, there should be absolutely no repetition, and it is very flexible. I just don't know if there will be the problems they said about low efficiency and dead loops. Currently, the running condition is good.
size]

Sorry, moderator. After repeated testing, your method on the 6th floor is okay, but there will be a dead loop. Maybe I tested too many methods and got the wrong object. I'm sorry. The method modified on the 42nd floor I think is close to perfect. There are so many good methods, batch processing is really a challenge to people's thinking
.

[ Last edited by 26933062 on 2007-1-6 at 10:07 PM ]
Floor 2 Posted 2007-01-04 20:42 ·  中国 河南 郑州 电信
中级用户
★★
Credits 439
Posts 170
Joined 2006-01-09 20:29
20-year member
UID 48707
Status Offline
5 digits? 5-digit?
1-100 has 5 digits
bat c c++
Floor 3 Posted 2007-01-04 22:17 ·  中国 广西 南宁 联通
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
5 randomly selected numbers from 1 to 99

1:
@echo off
:loop
cls
set random1=%random:~-2%

:loop2
set random2=%random:~-2%
if "%random2%" == "%random1%" goto loop2

:loop3
set random3=%random:~-2%
if "%random3%" == "%random1%" goto loop3
if "%random3%" == "%random2%" goto loop3

:loop4
set random4=%random:~-2%
if "%random4%" == "%random1%" goto loop4
if "%random4%" == "%random2%" goto loop4
if "%random4%" == "%random3%" goto loop4

:loop5
set random5=%random:~-2%
if "%random5%" == "%random1%" goto loop5
if "%random5%" == "%random2%" goto loop5
if "%random5%" == "%random3%" goto loop5
if "%random5%" == "%random4%" goto loop5

echo %random1% %random2% %random3% %random4% %random5%
pause
goto loop


2:
@echo off
:Start
cls
call :loop 1
call :loop 2
call :loop 3
call :loop 4
call :loop 5

echo %random1% %random2% %random3% %random4% %random5%
pause
set Tmp_=
goto :Start

:loop
set randomloop=%random:~-2%
for %%n in (%Tmp_%) do if "%randomloop%" == "%%n" goto loop
set random%1=%randomloop%
call set Tmp_=%%Tmp_%% %randomloop%
goto :eof


[ Last edited by zh159 on 2007-1-4 at 10:09 AM ]
Floor 4 Posted 2007-01-04 23:45 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
Come to join in the "fun"

@echo off
setlocal enabledelayedexpansion
set /a a=1

:test
set /a _num!a!=%random:~-2%
if not defined !_num%a%! set /a a+=1
if !a! LSS 6 goto :test

echo %_num1% %_num2% %_num3% %_num4% %_num5%

pause>nul
Floor 5 Posted 2007-01-05 01:52 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
The expression of the owner is problematic. After reading for a long time, I still don't know whether it is to generate 1-99 different random 5-digit numbers or to randomly display 5 different numbers in 1-99. Please make the owner's expression clear.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 6 Posted 2007-01-05 02:16 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
After looking at the code on floor 4, it is inferred that the original intention of youxi01 was to display 5 different random numbers between 1 and 99 (excluding 99), but there is a problem with the code: %a% is a number from 1 to 5, and using if not defined to judge whether the variable num%a% has been defined, the execution result is always not defined.

Modify the code according to the idea of floor 4 as follows:

@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

——————————————————————————————————————
If this code is called in a loop, it will fall into an infinite loop at the 20th time. For the specific situation, please refer to the description in floor 16. For the cause analysis, please refer to floor 41, and for the code to solve the problem, please refer to floor 42.

[ Last edited by namejm on 2007-1-6 at 05:03 PM ]
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 7 Posted 2007-01-05 03:05 ·  中国 山东 济南 电信
社区乞丐
★★
此图片另存后死机
Credits -49
Posts 90
Joined 2006-12-02 13:00
19-year member
UID 72412
Gender Male
Status Offline
Doubt about my own comprehension ability and the LZ's expression ability

Is it 5 numbers or 5 digits? 5 numbers from 1 to 100 as follows:


@ECHO %dbg% OFF
setlocal ENABLEDELAYEDEXPANSION

FOR /L %%A IN (1,1,5) DO ECHO !RANDOM:~-2!
我的网络笔记本.非联系本人请勿访问!http://w.vicp.net
Floor 8 Posted 2007-01-05 03:33 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
I have discovered that many people have overlooked such a detail: %random:~-2% might take the string "00". Although set /a num=%random:~-2% can convert the "00" string into a numerical value, turning it into 0, however, 0 is not what the original poster wanted.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 9 Posted 2007-01-05 04:30 ·  中国 山东 济南 电信
社区乞丐
★★
此图片另存后死机
Credits -49
Posts 90
Joined 2006-12-02 13:00
19-year member
UID 72412
Gender Male
Status Offline
Okay,
%random:~-2% doesn't work, then %random:~0,2%

Ensure there's no 0 in front


@ECHO %dbg% OFF
setlocal ENABLEDELAYEDEXPANSION

FOR /L %%A IN (1,1,5) DO ECHO !RANDOM:~0,2!
我的网络笔记本.非联系本人请勿访问!http://w.vicp.net
Floor 10 Posted 2007-01-05 04:36 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Do the 5 numbers obtained include duplicates?
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 11 Posted 2007-01-05 04:41 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
According to the building楼主's statement, there should be no duplicate numbers among the 5 numbers.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 12 Posted 2007-01-05 04:43 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
At present, this situation has been found on the 9th floor.
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 13 Posted 2007-01-05 06:18 ·  中国 山东 济南 电信
社区乞丐
★★
此图片另存后死机
Credits -49
Posts 90
Joined 2006-12-02 13:00
19-year member
UID 72412
Gender Male
Status Offline
OK!!!Nested recursion! This time it won't repeat, right??? Hehe

@ECHO %dbg% OFF
setlocal ENABLEDELAYEDEXPANSION
SET T=1

:PR
FOR /L %%A IN (%T%,1,5) DO (SET NO%%A=!RANDOM:~0,2! & SET /A H=%%A-1 & IF !H! GTR -1 (
FOR /L %%B IN (!H!,-1,1) DO (IF !NO%%A!==!NO%%B! (SET T=%%A & CALL :PR !T! & GOTO :EOF)))&ECHO !NO%%A!)



[ Last edited by tghksj on 2007-1-4 at 06:11 PM ]
我的网络笔记本.非联系本人请勿访问!http://w.vicp.net
Floor 14 Posted 2007-01-05 06:44 ·  中国 山东 济南 电信
社区乞丐
★★
此图片另存后死机
Credits -49
Posts 90
Joined 2006-12-02 13:00
19-year member
UID 72412
Gender Male
Status Offline
Oh my god~~ I'm stupid, why didn't I think of if not defined
Dizzy dizzy dizzy dizzy dizzy dizzy dizzy dizzy dizzy dizzy dizzy dizzy dizzy dizzy dizzy


@ECHO %dbg% OFF
setlocal ENABLEDELAYEDEXPANSION
CLS
SET T=1

:PR
FOR /L %%A IN (%T%,1,5) DO (
SET NO%%A=!RANDOM:~0,2!
IF DEFINED !NO%%A! (SET T=!%%A! & GOTO :PR)
ECHO !NO%%A!
)


Hey?????????? Doesn't work????????????? There are still duplicates??!
Experts explain it...

Only the 13th floor is available for the time being....

[ Last edited by tghksj on 2007-1-4 at 05:53 PM ]
我的网络笔记本.非联系本人请勿访问!http://w.vicp.net
Floor 15 Posted 2007-01-05 06:58 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
After looking at the code on floor 14, I found that there is no statement to control whether random numbers are repeated. After testing several times, you will find that repeated values appear. It is recommended to refer to the code I posted on floor 6.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Forum Jump: