![]() |
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-08-29 08:05 |
47,816 topics / 349,916 posts / today 0 new / 48,265 members |
| DOS批处理 & 脚本技术(批处理室) » If I need to generate a random number between 1 and 100, how should I write it (completed) |
| Printable Version 4,094 / 16 |
| Floor1 mwh79 | Posted 2006-12-01 04:41 |
| 初级用户 Posts 27 Credits 86 | |
|
If I need to generate a random number between 1 and 100, how should I write it?
Ask for advice. Is there anyone who knows? Use BAT to implement it!!! Do not use %random%, but only select from 1 to 100, and improve efficiency! Got it, thank you. [ Last edited by mwh79 on 2006-12-1 at 07:13 AM ] |
|
| Floor2 NaturalJ0 | Posted 2006-12-01 04:47 |
| 银牌会员 Posts 533 Credits 1,181 | |
|
set /a RandomNumber=%random%%%100+1 (used in bat)
|
|
| Floor3 mwh79 | Posted 2006-12-01 04:51 |
| 初级用户 Posts 27 Credits 86 | |
|
set /a RandomNumber=%random%%%100+1
It doesn't run! This doesn't seem very efficient! It takes too long to get the number! [ Last edited by mwh79 on 2006-12-1 at 04:53 AM ] |
|
| Floor4 zh159 | Posted 2006-12-01 05:05 |
| 金牌会员 Posts 1,467 Credits 3,687 | |
|
%random% is a randomly appearing number, and it's instantaneous. If your machine is slow, that should be an issue with your machine
|
|
| Floor5 mwh79 | Posted 2006-12-01 05:10 |
| 初级用户 Posts 27 Credits 86 | |
|
No, it's mainly a matching problem. If you want to generate a random number between 95 and 97, then it's possible that it takes a long time to get it, it depends on luck. So I want to fix it within a range!
|
|
| Floor6 zh159 | Posted 2006-12-01 05:38 |
| 金牌会员 Posts 1,467 Credits 3,687 | |
|
This approach reminds me of the "problem about randomly getting file names" discussed before.
Take a close look, it's very helpful. http://www.cn-dos.net/forum/viewthread.php?tid=22128 |
|
| Floor7 mwh79 | Posted 2006-12-01 05:51 |
| 初级用户 Posts 27 Credits 86 | |
|
Thanks..I took a look,,
But it's still not what I need,, Their random numbers all start from 1,,,Can we implement a statement that starts from 35 and ends at 100, so as to skip 1~35, which should be more efficient,,,I'm worried that with random numbers from 98~100, when will the luck come to generate that! |
|
| Floor8 zh159 | Posted 2006-12-01 05:56 |
| 金牌会员 Posts 1,467 Credits 3,687 | |
|
You haven't looked carefully enough. Using %random% simply to obtain random numbers in a certain range has an efficiency issue. The problem is how you use %random%.
|
|
| Floor9 mwh79 | Posted 2006-12-01 06:02 |
| 初级用户 Posts 27 Credits 86 | |
|
Can %random% obtain a certain range?,,, I have tried many parameters,,, now I can control the maximum value, but I can't control the minimum value
|
|
| Floor10 zh159 | Posted 2006-12-01 06:07 |
| 金牌会员 Posts 1,467 Credits 3,687 | |
|
```@echo off
:loop set /a RandomNumber=1%random:~-1%%random:~-1%-99 ::Maximum value 100 if %RandomNumber% LSS 35 goto loop ::Return if less than 35 if %RandomNumber% GTR 75 goto loop ::Return if greater than 75 echo %RandomNumber% if "%RandomNumber%" == "50" pause goto loop ``` |
|
| Floor11 mwh79 | Posted 2006-12-01 06:22 |
| 初级用户 Posts 27 Credits 86 | |
|
Dude, how about the efficiency of yours??
:STARTIP Set /a a=%RANDOM%%%250 IF %a% LEQ 100 goto startip IF %a% GEQ 105 goto startip Mine like this is very inefficient |
|
| Floor12 zh159 | Posted 2006-12-01 06:24 |
| 金牌会员 Posts 1,467 Credits 3,687 | |
Originally posted by mwh79 at 2006-11-30 18:22: Result comes from testing |
|
| Floor13 mwh79 | Posted 2006-12-01 06:31 |
| 初级用户 Posts 27 Credits 86 | |
|
I tested it and it's the same. Actually, what I want to achieve is not to display, but to only read the numbers from 35 to 100, and not read 1 to 34. In this way, I can get the data immediately without judgment!
|
|
| Floor14 tianzizhi | Posted 2006-12-01 06:39 |
| 高级用户 Posts 214 Credits 623 | |
|
Take numbers within a certain range,
@echo off :: 22 is the range number, 45 is the base number, that is, the minimum number required by the owner. For example, if you want to take numbers between 45-67, please pay attention to the format. If it is wrong, it will go wrong. The owner said that it did not run successfully because the format was wrong. Also, to take a range of numbers, you need to implement it with batch processing. It will go wrong in cmd. It is okay to take a random number in cmd, that is, just write %random%. set /a c=%random% %% 22+45 echo %c% :: Take a random two-digit number set /a d=%random:~0,2% echo %d% :: Avoid 00 set /a e=1%random:~0,2%-100 echo %e% |
|
| Floor15 mwh79 | Posted 2006-12-01 06:53 |
| 初级用户 Posts 27 Credits 86 | |
|
That's good, thank you. I know how to write the minimum value...
There's another question set /a c=%random% %% 22+45 Can the two numbers in this segment be written as variables?? Because this is too inflexible and too rigid, and it's too cumbersome to modify manually.... |
|
| 1 2 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |