|
zh159
金牌会员
     
积分 3687
发帖 1467
注册 2005-8-8
状态 离线
|
『第 46 楼』:
使用 LLM 解释/回答一下
用“set /a randomloop=(%random%+%random%+%random%)%%100+1”增加随机性,也可以用“set /a randomloop=(%random%+%random%+%random%)/3%%100+1”
@echo off
set Max=100
set N=5
:start
cls
echo %time%
set Num=
set Tmp$=
:loop
set /a randomloop=(%random%+%random%+%random%)/3%%%Max%+1
for %%n in (%Tmp$%) do if "%randomloop%" == "%%n" goto loop
call set Tmp$=%%Tmp$%% %randomloop%
set /a Num+=1
set random_%Num%=%randomloop%
if %Num% LSS %N% goto loop
echo %Tmp$%
echo %time%
pause
goto start
exit
Use "set /a randomloop=(%random%+%random%+%random%)%%100+1" to increase randomness, or you can use "set /a randomloop=(%random%+%random%+%random%)/3%%100+1"
@echo off
set Max=100
set N=5
:start
cls
echo %time%
set Num=
set Tmp$=
:loop
set /a randomloop=(%random%+%random%+%random%)/3%%%Max%+1
for %%n in (%Tmp$%) do if "%randomloop%" == "%%n" goto loop
call set Tmp$=%%Tmp$%% %randomloop%
set /a Num+=1
set random_%Num%=%randomloop%
if %Num% LSS %N% goto loop
echo %Tmp$%
echo %time%
pause
goto start
exit
|
|
2007-1-6 15:28 |
|
|
9527
银牌会员
     努力做坏人
积分 1185
发帖 438
注册 2006-8-28 来自 北京
状态 离线
|
『第 47 楼』:
使用 LLM 解释/回答一下
这几天看你们讨论这个帖子这么热闹,自己也忍不住试一把,以下代码测试100次没有出现问题,但不敢保证啊,还是请有心人测试一下才好
@echo off&setlocal enabledelayedexpansion
for /l %%a in (1;1;5) do (
set/a b=!random!%%99+1
call :pp !b!
)
goto :eof
:pp
if not defined %1 (set %1=a) else goto loop
echo %1
goto :eof
:loop
set/a b=!random!%%99+1
call :pp !b!
These days I see you guys discussing this post so lively, and I couldn't help but try it myself. The following code has been tested 100 times without problems, but I can't guarantee it. Still, it's better for interested people to test it.
@echo off&setlocal enabledelayedexpansion
for /l %%a in (1;1;5) do (
set/a b=!random!%%99+1
call :pp !b!
)
goto :eof
:pp
if not defined %1 (set %1=a) else goto loop
echo %1
goto :eof
:loop
set/a b=!random!%%99+1
call :pp !b!
此帖被 +4 点积分 点击查看详情 评分人:【 lxmxn 】 | 分数: +4 | 时间:2007-1-7 01:18 |
|
|

我今后在论坛的目标就是做个超级坏人!!! |
|
2007-1-6 23:47 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 48 楼』:
使用 LLM 解释/回答一下
9527 的代码测试通过,效果也不错,每次的数都很“随机”,加分。
The code for 9527 has passed the test and the effect is good. Each number is very "random", plus points.
|
|
2007-1-7 01:18 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 49 楼』:
使用 LLM 解释/回答一下
Originally posted by dikex at 2007-1-6 01:41:
嗯,有道理……
又一个猜想出现了^_^
可能时间只是产生时的其中一个参数,还与上一个随机数有关,而上一个经过某些放大差别以及和时间相关的迠...
兄的发散思维令人佩服。
Originally posted by dikex at 2007-1-6 01:41:
Well, that makes sense...
Another conjecture emerges ^_^
Maybe time is just one of the parameters at the time of generation, and it is also related to the previous random number, and the previous one has gone through some amplification differences and...
Your divergent thinking is admirable.
|
|
2007-1-7 01:19 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 50 楼』:
使用 LLM 解释/回答一下
那我也再帖一段代码,不用 defined 来控制重复值,速度有点慢:
@echo off
:begin
cls
set count=0
:loop
set /a num=%random%%%100+1
echo %str%|find " %num% ">nul&&goto loop
set str=%str% %num%
set /a count+=1
if %count% lss 5 goto loop
echo %str%
set str=
pause
goto begin
————————————————————————————————————
原来 dikex 兄在43楼已经发过这样的代码,看来我是发重了。不过还是保留一下吧,感觉这个要稍微完善一点,没有和 dikex 兄争版权的意思。
Last edited by namejm on 2007-1-6 at 05:17 PM ]
Then I'll post another piece of code. Without using defined to control duplicate values, it's a bit slow:
@echo off
:begin
cls
set count=0
:loop
set /a num=%random%%%100+1
echo %str%|find " %num% ">nul&&goto loop
set str=%str% %num%
set /a count+=1
if %count% lss 5 goto loop
echo %str%
set str=
pause
goto begin
————————————————————————————————————
Originally, Brother dikex had already posted such code on floor 43. It seems I repeated it. But I'll still keep it. I feel this one is a bit more refined. There's no intention to compete for copyright with Brother dikex.
Last edited by namejm on 2007-1-6 at 05:17 PM ]
|

尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2007-1-7 01:26 |
|
|
tghksj
社区乞丐
此图片另存后死机
积分 -49
发帖 90
注册 2006-12-2
状态 离线
|
『第 51 楼』:
使用 LLM 解释/回答一下
13楼产生重复的原因主要出在是取%random%的前两位.
把SET NO%%A=!RANDOM:~0,2! 换成SET /A NO%%A=!RANDOM! %% 100
就不成问题了.
其实对于这个问题已经没有什么好继续讨论的了,32楼的代码已经完全解释并解决了.
13楼的代码与其相比简直就是一个笑话....
The main reason for the duplication in floor 13 mainly lies in taking the first two digits of %random%.
Change SET NO%%A=!RANDOM:~0,2! to SET /A NO%%A=!RANDOM! %% 100, and there will be no problem.
In fact, there is no need to continue discussing this problem anymore. The code in floor 32 has completely explained and solved it.
The code in floor 13 is simply a joke compared with it.
|

我的网络笔记本.[color=Red]非联系本人请勿访问![/color]http://w.vicp.net[img]http://zhenlove.com.cn/cndos/fileup/files/QM2.jpg[/img] |
|
2007-1-7 02:14 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 52 楼』:
使用 LLM 解释/回答一下
Originally posted by tghksj at 2007-1-6 13:14:
其实对于这个问题已经没有什么好继续讨论的了,32楼的代码已经完全解释并解决了.
同一问题,可以换用不同的思路来解决,最终是殊途同归,但精彩部分在于解决问题的过程而不是最终结果;或者根本得不到完美的结果,但是解决问题的过程却能给他人以启发。只要还有不同的解法,继续讨论下去还是有好处的。
Originally posted by tghksj at 2007-1-6 13:14:
Actually, there is no need to continue discussing this issue anymore, as the code on floor 32 has completely explained and solved it.
The same problem can be solved with different ideas, and in the end, they lead to the same result. But the wonderful part is the process of solving the problem, not the final result; or sometimes there may not be a perfect result, but the process of solving the problem can inspire others. As long as there are different solutions, it is still beneficial to continue discussing.
|

尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2007-1-7 02:33 |
|
|
tghksj
社区乞丐
此图片另存后死机
积分 -49
发帖 90
注册 2006-12-2
状态 离线
|
   『第 53 楼』:
使用 LLM 解释/回答一下
对32楼qzwqzw老师代码的剖析:
以0-3为范围,取0-3之间的随机。
代码修改如下:
@echo off
setlocal EnableDelayedExpansion
:: 初始化顺序数列
for /l %%i in (0,1,3) do (
set rnum%%i=%%i
)
:: 对数列进行随机交换
for /l %%i in (0,1,3) do (
set /a rnd=!random! %% 3
call set tmp=%%rnum!rnd!%%
set rnum!rnd!=!rnum%%i!
set rnum%%i=!tmp!
)
set rnum
逐行分析如下:
------------------------------------------
第一个FOR循环(0-3),四次
关键句:set rnum%%i=%%i
rnum0=0
rnum1=1
rnum2=2
rnum3=3
------------------------------------------
第二个FOR循环(0-3),四次
----------第一次:---------
关键句1: set /a rnd=!random! %% 3
运行结果: rnd=1
( !random! %% 3 的值为0-3间任意整数" 这里假设为1"
作用:将0-3间任意整数赋值给变量rnd )
关键句2: call set tmp=%%rnum!rnd!%%
运行结果: tmp=1
(因为此时 !rnd! 值为1 ,rnum!rnd!就是rnum1
此时 rnum1 值为1 ,所以%%rnum!rnd!%%值为1,所以tmp=1.
注意:rnum 并不是一个变量,这里只是一段字符串,与!rnd!组合后成为变量名
作用:和关键句1配合,将第一个循环的产生的任意一个变量的"值"赋给变量tmp)
关键句3: set rnum!rnd!=!rnum%%i!
运行结果: rnum1=0
(因为此时!rnd! 值为1,rnum!rnd!就是rnum1,
此时%%i 值为0,rnum%%i 就是rnum0,
此时!rnum0! 值为0,所以rnum1=0
注意:此时对第一个FOR循环中产生的变量的值进行了修改如下:
rnum0=0
rnum1=0
rnum2=2
rnum3=3
作用:和关键句1配合,随机抽取第一个FOR循环产生的变量名,并为其赋值为
"与第二个FOR循环,'此次循环次数'相对应的,第一个FOR循环内产生的变量的'值'")
关键句4: set rnum%%i=!tmp!
运行结果: rnum0=1
(此时对第一个FOR循环中产生的变量的值进行了修改如下:
rnum0=1
rnum1=0
rnum2=2
rnum3=3
看到这里就豁然开朗了,后面的3次循环也就不用再看了,
正是作者在原代码在第2个FOR循环前注释的:
作用: 与关键句1.2.3配合,对数列进行随机交换.
------------------------
当我在致力解决%random%出相同值的时候,
qzwqzw老师在第一个循环开始的时候就已经定义了其"不重复性"
目的值并不是%random%值,%random%只不过起到了随机互换的作用.......哎.........
qzwqzw老师每出一段代码我几乎都要看上20几个小时,
已至于我晚上睡觉在梦里满脑子都在想其算法机理,差距之大,难以想象...
Last edited by tghksj on 2007-1-6 at 03:57 PM ]
Analysis of Teacher qzwqzw's Code on Floor 32:
Take a random number between 0-3 with the range of 0-3.
The code is modified as follows:
[CODE]
@echo off
setlocal EnableDelayedExpansion
:: Initialize the sequence
for /l %%i in (0,1,3) do (
set rnum%%i=%%i
)
:: Randomly swap the sequence
for /l %%i in (0,1,3) do (
set /a rnd=!random! %% 3
call set tmp=%%rnum!rnd!%%
set rnum!rnd!=!rnum%%i!
set rnum%%i=!tmp!
)
set rnum
[/CODE]
Analyze line by line as follows:
[CODE]
------------------------------------------
First FOR loop (0-3), four times
Key sentence: set rnum%%i=%%i
rnum0=0
rnum1=1
rnum2=2
rnum3=3
------------------------------------------
Second FOR loop (0-3), four times
----------First time:---------
Key sentence 1: set /a rnd=!random! %% 3
Running result: rnd=1
(!random! %% 3 takes any integer between 0-3 "here it is assumed to be 1"
Function: Assign any integer between 0-3 to variable rnd )
Key sentence 2: call set tmp=%%rnum!rnd!%%
Running result: tmp=1
(Because at this time!rnd! is 1, rnum!rnd! is rnum1
At this time rnum1 is 1, so %%rnum!rnd!%% is 1, so tmp=1.
Note: rnum is not a variable, here it is just a string, combined with!rnd! to become a variable name
Function: Cooperate with key sentence 1 to assign the "value" of any variable generated by the first loop to variable tmp)
Key sentence 3: set rnum!rnd!=!rnum%%i!
Running result: rnum1=0
(Because at this time!rnd! is 1, rnum!rnd! is rnum1,
At this time%%i is 0, rnum%%i is rnum0,
At this time!rnum0! is 0, so rnum1=0
Note: At this time, the values of the variables generated in the first FOR loop are modified as follows:
rnum0=0
rnum1=0
rnum2=2
rnum3=3
Function: Cooperate with key sentence 1 to randomly extract the variable name generated in the first FOR loop and assign it the "value" of the variable generated in the first FOR loop corresponding to "the current loop number" in the second FOR loop)
Key sentence 4: set rnum%%i=!tmp!
Running result: rnum0=1
(At this time, the values of the variables generated in the first FOR loop are modified as follows:
rnum0=1
rnum1=0
rnum2=2
rnum3=3
Now it becomes clear, there is no need to look at the next 3 loops. It is exactly the comment made by the author before the second FOR loop:
Function: Cooperate with key sentences 1, 2, and 3 to randomly swap the sequence.
[/CODE]
------------------------
When I was trying to solve the problem of the same value of %random%,
Teacher qzwqzw had already defined its "non-repeatability" at the beginning of the first loop.
The target value is not the value of %random%, %random% just plays the role of random swapping....... Alas.........
I almost spend more than 20 hours reading each code of Teacher qzwqzw,
So that I have this algorithm mechanism in my mind even when I sleep at night, the gap is so big that it is unimaginable...
[[i] Last edited by tghksj on 2007-1-6 at 03:57 PM [/i]]
此帖被 -11 点积分 点击查看详情 评分人:【 9527 】 | 分数: -11 | 时间:2007-1-19 21:43 |
|
|

我的网络笔记本.[color=Red]非联系本人请勿访问![/color]http://w.vicp.net[img]http://zhenlove.com.cn/cndos/fileup/files/QM2.jpg[/img] |
|
2007-1-7 04:55 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 54 楼』:
使用 LLM 解释/回答一下
实际上,qzwqzw 置换的模型是:假设有两个数A和B,现在要对它们的值互换,则设置一个临时变量tmp,先把A的值赋予tmp,然后,再把A的值设置为B的值,最后,再把B的值设置为tmp,这样,就实现了A和B这两个变量值的互换。
因为最开始已经生成了rnd0、rnd1……rnd99共100个变量的值,这些变量的值是从0到99递增的;后一个 for 语句再用random来生成0到99的随机的rnd,以保证能随机抽取rnd0、rnd1……rnd99中的任一变量来置换,从而保证了数值的不重复性。整体的思路就是先顺后乱,以达到随机的目的。
Actually, the model of the qzwqzw swap is: Suppose there are two numbers A and B. Now, to swap their values, set a temporary variable tmp. First, assign the value of A to tmp, then set the value of A to the value of B, and finally set the value of B to tmp. In this way, the values of the two variables A and B are swapped.
Because 100 variables from rnd0 to rnd99 have been generated at the beginning, and their values are increasing from 0 to 99; the subsequent for statement uses random to generate a random rnd from 0 to 99 to ensure that any variable among rnd0, rnd1... rnd99 can be randomly selected for swapping, thus ensuring the non - repeatability of values. The overall idea is to first be in order and then be disordered to achieve the purpose of randomness.
|

尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2007-1-7 05:47 |
|
|
zh159
金牌会员
     
积分 3687
发帖 1467
注册 2005-8-8
状态 离线
|
『第 55 楼』:
使用 LLM 解释/回答一下
qzwqzw 的随机方法不错
不过感觉一次要设置这么多的变量在只获取少量随机数时会造成运行效率下降:
:: 生成0-99之间的随机数列 R1
:: qzwqzw@bbs.cn-dos.net
:: 2007-01-04 23:03
@echo off
setlocal EnableDelayedExpansion
:: 初始化顺序数列
for /l %%i in (0,1,99) do (
set rnum%%i=%%i
)
:: 对数列进行随机交换
:loop
set time1=%time%
if not "%time1:~-2%" == "00" goto loop
cls
for /l %%i in (0,1,99) do (
set /a rnd=!random! %% 100
call set tmp=%%rnum!rnd!%%
set rnum!rnd!=!rnum%%i!
set rnum%%i=!tmp!
)
echo %rnum1% %rnum2% %rnum3% %rnum4% %rnum5%
echo %time1%
echo %time%
pause
goto loop
62 37 16 28 42
0:59:47.00
0:59:47.18
请按任意键继续. . .
@echo off
set Max=100
set N=5
:start
set time1=%time%
if not "%time1:~-2%" == "00" goto start
cls
set Num=
set Tmp$=
:loop
set /a randomloop=(%random%+%random%+%random%)/3%%%Max%+1
for %%n in (%Tmp$%) do if "%randomloop%" == "%%n" goto loop
call set Tmp$=%%Tmp$%% %randomloop%
set /a Num+=1
set random_%Num%=%randomloop%
if %Num% LSS %N% goto loop
echo %Tmp$%
echo %time1%
echo %time%
pause
goto start
exit
67 5 57 82 15
0:59:29.00
0:59:29.01
请按任意键继续. . .
Last edited by zh159 on 2007-1-19 at 01:01 AM ]
qzwqzw's random method is good
But it feels that setting so many variables at one time will cause a decrease in running efficiency when only getting a small number of random numbers:
:: Generate a random sequence R1 between 0-99
:: qzwqzw@bbs.cn-dos.net
:: 2007-01-04 23:03
@echo off
setlocal EnableDelayedExpansion
:: Initialize the sequential sequence
for /l %%i in (0,1,99) do (
set rnum%%i=%%i
)
:: Randomly swap the sequence
:loop
set time1=%time%
if not "%time1:~-2%" == "00" goto loop
cls
for /l %%i in (0,1,99) do (
set /a rnd=!random! %% 100
call set tmp=%%rnum!rnd!%%
set rnum!rnd!=!rnum%%i!
set rnum%%i=!tmp!
)
echo %rnum1% %rnum2% %rnum3% %rnum4% %rnum5%
echo %time1%
echo %time%
pause
goto loop
62 37 16 28 42
0:59:47.00
0:59:47.18
Press any key to continue . . .
@echo off
set Max=100
set N=5
:start
set time1=%time%
if not "%time1:~-2%" == "00" goto start
cls
set Num=
set Tmp$=
:loop
set /a randomloop=(%random%+%random%+%random%)/3%%%Max%+1
for %%n in (%Tmp$%) do if "%randomloop%" == "%%n" goto loop
call set Tmp$=%%Tmp$%% %randomloop%
set /a Num+=1
set random_%Num%=%randomloop%
if %Num% LSS %N% goto loop
echo %Tmp$%
echo %time1%
echo %time%
pause
goto start
exit
67 5 57 82 15
0:59:29.00
0:59:29.01
Press any key to continue . . .
Last edited by zh159 on 2007-1-19 at 01:01 AM ]
|
|
2007-1-19 13:43 |
|
|
everest79
金牌会员
      一叶枝头,万树皆春
积分 2564
发帖 1127
注册 2006-12-25
状态 离线
|
『第 56 楼』:
使用 LLM 解释/回答一下
for /l %%i in (1,1,5) do call :ccc %%i
echo %l1% %l2% %l3% %l4% %l5%
pause
exit
:ccc
set /a l%1=(((%time:~-1%+%1)*9-1)*(%time:~6,1%+%1)*(%time:~-2%+%1)*(%time:~7,1%+%1)*%1)%%100+%time:~7,1%+%1
```
for /l %%i in (1,1,5) do call :ccc %%i
echo %l1% %l2% %l3% %l4% %l5%
pause
exit
:ccc
set /a l%1=(((%time:~-1%+%1)*9-1)*(%time:~6,1%+%1)*(%time:~-2%+%1)*(%time:~7,1%+%1)*%1)%%100+%time:~7,1%+%1
```
|
|
2007-1-19 22:27 |
|
|
stornager
中级用户
   scriptlover
积分 328
发帖 131
注册 2007-3-25
状态 离线
|
『第 57 楼』:
使用 LLM 解释/回答一下
set random1=%random:~-2%
代码中%random:~-2%这个变两‘量有何含义?????????
In the code, `%random:~-2%` is used to take the last two digits of the random number generated by the `%random%` variable. The `~-2` is a substring extraction operation in batch scripting, which extracts the last two characters of the string generated by `%random%`.
|
|
2007-4-3 05:43 |
|
|
flyinspace
银牌会员
    
积分 1206
发帖 517
注册 2007-3-25
状态 离线
|
『第 58 楼』:
使用 LLM 解释/回答一下
同时显示5个数字哦。
还是5位数??
Display 5 numbers at the same time. Still 5-digit number??
|

知,不觉多。不知,乃求知 |
|
2007-4-3 07:41 |
|