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:24
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Randomly display 5 different numbers View 6,596 Replies 57
Floor 46 Posted 2007-01-06 15:28 ·  中国 河南 许昌 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
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
Floor 47 Posted 2007-01-06 23:47 ·  中国 北京 联通
银牌会员
★★★
努力做坏人
Credits 1,185
Posts 438
Joined 2006-08-28 12:00
19-year member
UID 61449
From 北京
Status Offline
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!

Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
lxmxn +4 2007-01-07 01:18
我今后在论坛的目标就是做个超级坏人!!!
Floor 48 Posted 2007-01-07 01:18 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

  The code for 9527 has passed the test and the effect is good. Each number is very "random", plus points.
Floor 49 Posted 2007-01-07 01:19 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
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.
Floor 50 Posted 2007-01-07 01:26 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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没商量。
考虑问题复杂化,解决问题简洁化。
Floor 51 Posted 2007-01-07 02:14 ·  中国 山东 济南 电信
社区乞丐
★★
此图片另存后死机
Credits -49
Posts 90
Joined 2006-12-02 13:00
19-year member
UID 72412
Gender Male
Status Offline
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.
我的网络笔记本.非联系本人请勿访问!http://w.vicp.net
Floor 52 Posted 2007-01-07 02:33 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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没商量。
考虑问题复杂化,解决问题简洁化。
Floor 53 Posted 2007-01-07 04:55 ·  中国 山东 济南 电信
社区乞丐
★★
此图片另存后死机
Credits -49
Posts 90
Joined 2006-12-02 13:00
19-year member
UID 72412
Gender Male
Status Offline
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:

@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


Analyze line by line as follows:

------------------------------------------
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.

------------------------
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...

[ Last edited by tghksj on 2007-1-6 at 03:57 PM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
9527 -11 2007-01-19 21:43
我的网络笔记本.非联系本人请勿访问!http://w.vicp.net
Floor 54 Posted 2007-01-07 05:47 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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没商量。
考虑问题复杂化,解决问题简洁化。
Floor 55 Posted 2007-01-19 13:43 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
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 ]
Floor 56 Posted 2007-01-19 22:27 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
```
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

```
Floor 57 Posted 2007-04-03 05:43 ·  中国 湖北 武汉 电信
中级用户
★★
scriptlover
Credits 328
Posts 131
Joined 2007-03-25 22:17
19-year member
UID 82910
Gender Male
Status Offline
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%`.
Floor 58 Posted 2007-04-03 07:41 ·  中国 广东 广州 荔湾区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Display 5 numbers at the same time. Still 5-digit number??
知,不觉多。不知,乃求知
Forum Jump: