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:37
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Randomly display 5 different numbers View 6,599 Replies 57
Floor 31 Posted 2007-01-05 11:10 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
Thank you everyone! You've worked hard!!!
Floor 32 Posted 2007-01-05 12:15 ·  中国 江西 南昌 电信
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
Actually, this is a problem of generating a random sequence of P(m, n).

When m is much larger than n, the collision - based generation method by the above - mentioned people can be adopted.

But when n is close to m, the collision generation efficiency is extremely low.

At this time, the sequential sequence random sorting or sequential sequence random selection method is usually adopted.

The following is an example of P(100, 100), that is, generating 100 non - repeated random sequences between 0 - 99.


:: 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
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!
)

set rnum
pause
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
namejm +4 2007-01-05 12:25
26933062 +15 2008-07-07 19:24
Floor 33 Posted 2007-01-05 12:27 ·  中国 广西 南宁 联通
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
```
@echo off
:start
cls
set Num=
set Tmp$=

:loop
set /a randomloop=%random%%%99+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 5 goto loop

for /l %%n in (1,1,%Num%) do call set /p= %%random_%%n%%<nul
echo.
pause
goto start
exit

```

It can be displayed with "echo %Tmp$%" and also with "set random_".
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
26933062 +2 2007-01-06 12:29
Floor 34 Posted 2007-01-06 01:47 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

  After multiple tests, it was found that the code on the 13th floor is very strange. The first number among the five numbers in the result is very likely to be the same as the first number in the next test result. I don't know the reason. Maybe there is a problem with the code.

  The following are the results of my five consecutive tests:
Attachments
1111.BMP
Floor 35 Posted 2007-01-06 03:42 ·  中国 山东 济南 电信
社区乞丐
★★
此图片另存后死机
Credits -49
Posts 90
Joined 2006-12-02 13:00
19-year member
UID 72412
Gender Male
Status Offline
It's qzwqzw again, my idol~~

I can't understand floor 32~~~~~ I can't take it anymore... I have to look at each of your codes for a day or two...

Please explain each sentence one by one, okay??? Mommiya~~~ can't take it anymore~!!!

----------------------------------

You're lucky on floor 34, I didn't have such a situation here. Buy a lottery ticket today and try!! Definitely win!!!!!:)

[ Last edited by tghksj on 2007-1-5 at 03:01 PM ]
我的网络笔记本.非联系本人请勿访问!http://w.vicp.net
Floor 36 Posted 2007-01-06 04:29 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
Originally posted by tghksj at 2007-1-5 14:42:
Another idol of mine, qzwqzw~~

I can't understand the 32nd floor... I can't take it anymore... I have to read each of your codes for a day or two...

Please explain each sentence one by one, okay??? Mom, I can't take it anymore~!!!

--- ...


  Oh, how does this have anything to do with luck? I tested it countless times today, and it's always like this situation.
Floor 37 Posted 2007-01-06 09:01 ·  中国 广西 南宁 联通
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
Originally posted by lxmxn at 2007-1-5 12:47:

After multiple tests, I found that the code on floor 13 is very strange. The first number among the five numbers in the result is easily the same as the first number in the next test result. I don't know the reason. Maybe there is a problem with the code.

I tried it more than ten times, and it's the same after waiting for a few minutes and then trying again
Floor 38 Posted 2007-01-06 10:32 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
It is extremely simple to display a random number in the range of 1-100. The key issue is to exclude duplicate numbers, which adds some workload.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 39 Posted 2007-01-06 10:50 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
Yes, moderator, the code on your 6th floor, after multiple tests, I also found the phenomenon of duplication.
Floor 40 Posted 2007-01-06 12:01 ·  中国 福建 福州 连江县 电信
中级用户
★★
Credits 356
Posts 115
Joined 2004-07-27 00:00
21-year member
UID 29114
Gender Male
Status Offline
Personally, I think the 32nd floor is the only correct answer. First, establish all the ordered number sequences that meet the requirements, then randomly arrange the ordered sequences. Randomly taking any bit will definitely meet the requirements of no repetition and randomness. For other ideas, first take some random numbers that meet the requirements, then judge for repetition. Theoretically, there may be a possibility of an infinite loop.
Floor 41 Posted 2007-01-06 12:46 ·  中国 广东 电信
荣誉版主
★★★★
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 26933062 at 2007-1-5 21:50:
Yes, moderator, I tested the code on floor 6 many times and found that there are still duplicate phenomena.

   Is that so? I have used the if not defined statement to control whether the variable is duplicated to exclude duplicate values. Theoretically, there should be no duplicate values. Moreover, after I tested no less than 100 times, I never found duplicate values. How did you test and get duplicates many times?

  In addition, while replying to this post, I suddenly understood how the strange phenomenon described in my 16th floor was caused: originally, when the num%num% from num1 to num99, these 99 variables are all defined, the 5th value of the 20th time should be the 100th value. At this time, all available variable names have been defined, so the program can only spin between loop and goto loop, causing an infinite loop. We should clear all variables appropriately to solve the strange problem described in the 16th floor.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 42 Posted 2007-01-06 13:07 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Modify the code on floor 6 slightly so that the code can repeat N times without the phenomenon mentioned in floor 16:


@echo off
:begin
cls
set times=0
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%%
)
set /a times+=1
if %times% equ 20 (
for /l %%i in (1,1,99) do set num%%i=
set times=0
)
if %count% lss 5 goto loop
pause>nul
goto begin
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 43 Posted 2007-01-06 14:02 ·  中国 广东 广州 天河区 电信
高级用户
★★★
潜水修练批处理
Credits 788
Posts 366
Joined 2006-12-31 02:43
19-year member
UID 75048
Gender Male
Status Offline
Just join in the fun and post one too

@echo off
set count=1
set num=
echo %random% >nul
:DORND
set /a rnd=%random%*100/32767+1
echo %num% | find " %rnd% " && goto DORND
set num=%num% %rnd%
set /a count+=1
if %count% lss 6 goto DORND
echo %num%
pause



Everyone may feel strange when seeing the fourth line. For this, please see the following question from xmxn and my personal insights

Originally posted by lxmxn at 2007-1-5 12:47 PM:

  After multiple tests, it is found that the code on floor 13 is very strange. The first number among the five numbers in the result is very easy to be the same as the first number in the next test result. I don't know what the reason is..


This seems to be a problem with the method of generating random numbers. Directly run the following statement continuously for several times
@echo off
set count=1
:loop
echo %random% & echo.
set /a count+=1
if %count% lss 10 goto loop
pause


We will find that the first number that comes out gradually increases after several consecutive runs, and there is no problem later. If the time interval is longer, the increase of the first number will be larger. Is this method of generating random numbers related to time? The original random numbers are of similar sizes, and it is not surprising that the results obtained after certain operations are the same. If this is the case, discarding the first random number or using another operation can solve the problem
Floor 44 Posted 2007-01-06 14:25 ·  中国 湖北 武汉 电信
版主
★★★★★
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:02:
Just to join in the fun, I'll post one too



Everyone may find the fourth line strange. For this, please see the following question from xmxn and my personal insights



This seems to be a problem with the method of generating random numbers...


  There might be a problem with this conjecture?

  Look at the following code: According to reason, the execution speed of the for command is very fast, but the size change of the random numbers generated by the following code is relatively large. Does this rule out the saying that "the generation of random numbers is related to time"?

@echo off&setlocal enabledelayedexpansion
for /l %%a in (1,1,20) do echo !random!
pause
Floor 45 Posted 2007-01-06 14:41 ·  中国 广东 广州 天河区 电信
高级用户
★★★
潜水修练批处理
Credits 788
Posts 366
Joined 2006-12-31 02:43
19-year member
UID 75048
Gender Male
Status Offline
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. After the previous one undergoes some amplified differences and operations related to time, the second one is truly random.
In short, there is something wrong with the way %random% generates random numbers. Everyone should be careful when using it. Discard the first random number if necessary.

P.S. Heard that someone said that the way VB generates random numbers is also very special. I wonder if it's similar to this. Conjecture... Conjecture... Continue to conjecture...
Forum Jump: