![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-09-10 06:39 |
47,812 topics / 349,914 posts / today 0 new / 48,268 members |
| DOS批处理 & 脚本技术(批处理室) » Randomly display 5 different numbers |
| Printable Version 7,212 / 57 |
| Floor31 26933062 | Posted 2007-01-05 11:10 |
| 银牌会员 Posts 879 Credits 2,268 | |
|
Thank you everyone! You've worked hard!!!
|
|
| Floor32 qzwqzw | Posted 2007-01-05 12:15 |
| 银牌会员 Posts 636 Credits 2,343 | |
|
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. |
|
| Floor33 zh159 | Posted 2007-01-05 12:27 |
| 金牌会员 Posts 1,467 Credits 3,687 | |
|
```
@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_". |
|
| Floor34 lxmxn | Posted 2007-01-06 01:47 |
| 版主 Posts 4,938 Credits 11,386 | |
|
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 (233.04 KiB) |
|
| Floor35 tghksj | Posted 2007-01-06 03:42 |
| 社区乞丐 Posts 90 Credits -49 | |
|
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 ] |
|
| Floor36 lxmxn | Posted 2007-01-06 04:29 |
| 版主 Posts 4,938 Credits 11,386 | |
Originally posted by tghksj at 2007-1-5 14:42: Oh, how does this have anything to do with luck? I tested it countless times today, and it's always like this situation. |
|
| Floor37 zh159 | Posted 2007-01-06 09:01 |
| 金牌会员 Posts 1,467 Credits 3,687 | |
Originally posted by lxmxn at 2007-1-5 12:47: I tried it more than ten times, and it's the same after waiting for a few minutes and then trying again |
|
| Floor38 namejm | Posted 2007-01-06 10:32 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
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.
|
|
| Floor39 26933062 | Posted 2007-01-06 10:50 |
| 银牌会员 Posts 879 Credits 2,268 | |
|
Yes, moderator, the code on your 6th floor, after multiple tests, I also found the phenomenon of duplication.
|
|
| Floor40 balinger | Posted 2007-01-06 12:01 |
| 中级用户 Posts 115 Credits 356 | |
|
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.
|
|
| Floor41 namejm | Posted 2007-01-06 12:46 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
Originally posted by 26933062 at 2007-1-5 21:50: 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. |
|
| Floor42 namejm | Posted 2007-01-06 13:07 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
Modify the code on floor 6 slightly so that the code can repeat N times without the phenomenon mentioned in floor 16:
|
|
| Floor43 dikex | Posted 2007-01-06 14:02 |
| 高级用户 Posts 366 Credits 788 | |
|
Just join in the fun and post one too
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: This seems to be a problem with the method of generating random numbers. Directly run the following statement continuously for several times @echo off 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 |
|
| Floor44 lxmxn | Posted 2007-01-06 14:25 |
| 版主 Posts 4,938 Credits 11,386 | |
Originally posted by dikex at 2007-1-6 01:02: 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"? |
|
| Floor45 dikex | Posted 2007-01-06 14:41 |
| 高级用户 Posts 366 Credits 788 | |
|
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... |
|
| Prev 1 2 3 4 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |