|
26933062
银牌会员
    
积分 2268
发帖 879
注册 2006-12-19
状态 离线
|
『楼 主』:
随机显示5个不同的数
使用 LLM 解释/回答一下
能不能一次显示出1-100随机的5位数,并且不能有重复的.
想了很久,也搜索了论坛里的大部分帖子,好像都没有同时显示5位数的,关键是还要不能重复,
不好意识,对不起各位了,看来我的表达能力确实有问题.
我的目的是 显示1-100的随机数, 一次同时显示5个 , 并且显示的这5个数不能有重复的.这样说大家能理解了吗?
首先多谢各位的精彩答复,因本人的批处理水平实在太菜,有些高深的答复不太看的懂,所以若有错误的评价还请见谅.....................
1, 6楼的答复基本符号要求,但多次使用时偶而会出现死循环现象,不知什么原因.
2, 13楼的虽没有重复现象,但每次运行后的第一位数总是相同,总觉略有不妥之处.
3, 26楼的虽然也可用,但是觉得过于呆板,(对不起,可能用词不当,但想不出更婉转的词了,见谅!)比如我想显示6位或7,8,9..........位就太麻烦了,其实我的本意是先要求输入随机数的最大值,再输入显示的位数.
4, 个人感觉(从思路上来说)32楼的应该是最理想的,但可惜我看不懂,而32楼的这位兄弟又没把代码按要求写完,(一次显示了100个).
5, 我认为33楼的代码应该是最好的了,从代码上看,应该是绝对没重复,而且很灵活,只是不知道会不会出现他们说的效率低,和死循环.目前运行状况良好.size]
不好意识 版主 经反复测试,你6楼的方法没问题,只是会出现死循环,可能是测试的方法太多了,搞错了对象,对不起了. 在42楼修改后的方法我认为以接近完美,真是条条大路通罗马,好方法太多了,批处理真是对人思路的挑战.
Last edited by 26933062 on 2007-1-6 at 10:07 PM ]
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 ]
|
|
2007-1-4 16:12 |
|
|
a9319751
中级用户
  
积分 439
发帖 170
注册 2006-1-9
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
5个?5位?
1-100 有5位
5 digits? 5-digit?
1-100 has 5 digits
|

bat c c++ |
|
2007-1-4 20:42 |
|
|
zh159
金牌会员
     
积分 3687
发帖 1467
注册 2005-8-8
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
5随机个1-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 ]
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 ]
|
|
2007-1-4 22:17 |
|
|
youxi01
高级用户
   
积分 846
发帖 247
注册 2006-10-27 来自 湖南==》广东
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
来凑个“热闹”
@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
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
|
|
2007-1-4 23:45 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
楼主的表达有问题,看了半天还不知道究竟是要生成1-99个不同的随机5位数还是在1-99中随机显示5个不同的数,请楼主表达清楚。
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2007-1-5 01:52 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
看了4楼的代码,推测 youxi01 的本意是想显示5个 1-100 之间(不包含100)不同的随机数,但是代码是有问题的:%a% 是 1-5 的数,用 if not defined 来判断 num%a% 这个变量是否被定义过,执行的结果始终是没有定义过。
按照4楼的思路修改代码如下:
@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
——————————————————————————————————————
如果循环调用这段代码,在第20次的时候会陷入死循环,具体情况请看16楼的描述,原因分析请看41楼,解决问题的代码请看42楼。
Last edited by namejm on 2007-1-6 at 05:03 PM ]
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2007-1-5 02:16 |
|
|
tghksj
社区乞丐
此图片另存后死机
积分 -49
发帖 90
注册 2006-12-2
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
怀疑自己的理解能力和楼主表达能力
是5个 还是5位? 5个 1-100 如下:
@ECHO %dbg% OFF
setlocal ENABLEDELAYEDEXPANSION
FOR /L %%A IN (1,1,5) DO ECHO !RANDOM:~-2!
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:
[CODE]
@ECHO %dbg% OFF
setlocal ENABLEDELAYEDEXPANSION
FOR /L %%A IN (1,1,5) DO ECHO !RANDOM:~-2!
[/CODE]
|

我的网络笔记本.[color=Red]非联系本人请勿访问![/color]http://w.vicp.net[img]http://zhenlove.com.cn/cndos/fileup/files/QM2.jpg[/img] |
|
2007-1-5 03:05 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
发现很多人都忽略了这样一个细节:%random:~-2% 有可能取到 00 这样的字符串,set /a num=%random:~-2% 虽然可以把 00 字符串数值化,变成0,但是,0却并不是楼主所想要的。
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2007-1-5 03:33 |
|
|
tghksj
社区乞丐
此图片另存后死机
积分 -49
发帖 90
注册 2006-12-2
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
那OK,
%random:~-2%不行就%random:~0,2%
保证前面没0
@ECHO %dbg% OFF
setlocal ENABLEDELAYEDEXPANSION
FOR /L %%A IN (1,1,5) DO ECHO !RANDOM:~0,2!
Okay,
%random:~-2% doesn't work, then %random:~0,2%
Ensure there's no 0 in front
[CODE]
@ECHO %dbg% OFF
setlocal ENABLEDELAYEDEXPANSION
FOR /L %%A IN (1,1,5) DO ECHO !RANDOM:~0,2!
[/CODE]
|

我的网络笔记本.[color=Red]非联系本人请勿访问![/color]http://w.vicp.net[img]http://zhenlove.com.cn/cndos/fileup/files/QM2.jpg[/img] |
|
2007-1-5 04:30 |
|
|
ccwan
金牌会员
     
积分 2725
发帖 1160
注册 2006-9-23 来自 河北廊坊
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
取得的5个数中有相同的算不算?
Do the 5 numbers obtained include duplicates?
|

三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。 |
|
2007-1-5 04:36 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
按照楼主的说法,5个数中不能出现相同的数。
According to the building楼主's statement, there should be no duplicate numbers among the 5 numbers.
|

尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2007-1-5 04:41 |
|
|
ccwan
金牌会员
     
积分 2725
发帖 1160
注册 2006-9-23 来自 河北廊坊
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
目前9楼的已发现有这种情况。
At present, this situation has been found on the 9th floor.
|

三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。 |
|
2007-1-5 04:43 |
|
|
tghksj
社区乞丐
此图片另存后死机
积分 -49
发帖 90
注册 2006-12-2
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
OK!!!嵌套递归! 这下不重复了吧???呵呵
@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 ]
OK!!!Nested recursion! This time it won't repeat, right??? Hehe
[CODE]
@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!)
[/CODE]
[[i] Last edited by tghksj on 2007-1-4 at 06:11 PM [/i]]
|

我的网络笔记本.[color=Red]非联系本人请勿访问![/color]http://w.vicp.net[img]http://zhenlove.com.cn/cndos/fileup/files/QM2.jpg[/img] |
|
2007-1-5 06:18 |
|
|
tghksj
社区乞丐
此图片另存后死机
积分 -49
发帖 90
注册 2006-12-2
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
我晕~~我傻了,怎么没想起 if not defined
晕晕晕晕晕晕晕晕晕晕晕晕晕晕晕
@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!
)
咦?????????不好使?????????????还是有重复的??!
高人解释下..............
暂时只有13楼可以了....
Last edited by tghksj on 2007-1-4 at 05:53 PM ]
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
[CODE]
@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!
)
[/CODE]
Hey?????????? Doesn't work????????????? There are still duplicates??!
Experts explain it...
Only the 13th floor is available for the time being....
[[i] Last edited by tghksj on 2007-1-4 at 05:53 PM [/i]]
|

我的网络笔记本.[color=Red]非联系本人请勿访问![/color]http://w.vicp.net[img]http://zhenlove.com.cn/cndos/fileup/files/QM2.jpg[/img] |
|
2007-1-5 06:44 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
看了14楼的代码,发现里面并没有控制随机数是否重复的语句,多测试几次之后,就会发现有重复数值出现,建议参考一下我在6楼帖出来的代码。
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2007-1-5 06:58 |
|