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-07-15 01:24
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » If I need to generate a random number between 1 and 100, how should I write it (completed) View 3,848 Replies 16
Original Poster Posted 2006-12-01 04:41 ·  中国 上海 徐汇区 电信
初级用户
Credits 86
Posts 27
Joined 2005-09-25 13:58
20-year member
UID 42814
Status Offline
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 ]
Floor 2 Posted 2006-12-01 04:47 ·  中国 江苏 苏州 联通
银牌会员
★★★
Credits 1,181
Posts 533
Joined 2006-08-14 12:54
19-year member
UID 60484
Status Offline
set /a RandomNumber=%random%%%100+1 (used in bat)
Floor 3 Posted 2006-12-01 04:51 ·  中国 上海 徐汇区 电信
初级用户
Credits 86
Posts 27
Joined 2005-09-25 13:58
20-year member
UID 42814
Status Offline
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 ]
Floor 4 Posted 2006-12-01 05:05 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
%random% is a randomly appearing number, and it's instantaneous. If your machine is slow, that should be an issue with your machine
Floor 5 Posted 2006-12-01 05:10 ·  中国 上海 徐汇区 电信
初级用户
Credits 86
Posts 27
Joined 2005-09-25 13:58
20-year member
UID 42814
Status Offline
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!
Floor 6 Posted 2006-12-01 05:38 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
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
Floor 7 Posted 2006-12-01 05:51 ·  中国 上海 徐汇区 电信
初级用户
Credits 86
Posts 27
Joined 2005-09-25 13:58
20-year member
UID 42814
Status Offline
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!
Floor 8 Posted 2006-12-01 05:56 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
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%.
Floor 9 Posted 2006-12-01 06:02 ·  中国 上海 徐汇区 电信
初级用户
Credits 86
Posts 27
Joined 2005-09-25 13:58
20-year member
UID 42814
Status Offline
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
Floor 10 Posted 2006-12-01 06:07 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
```@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
```
Floor 11 Posted 2006-12-01 06:22 ·  中国 上海 徐汇区 电信
初级用户
Credits 86
Posts 27
Joined 2005-09-25 13:58
20-year member
UID 42814
Status Offline
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
Floor 12 Posted 2006-12-01 06:24 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
Originally posted by mwh79 at 2006-11-30 18:22:
Dude, how's your efficiency!!

:STARTIP
Set /a a=%RANDOM%%%250
IF %a% LEQ 100 goto startip
IF %a% GEQ 105 goto startip

Mine is very inefficient like this

Result comes from testing
Floor 13 Posted 2006-12-01 06:31 ·  中国 上海 徐汇区 电信
初级用户
Credits 86
Posts 27
Joined 2005-09-25 13:58
20-year member
UID 42814
Status Offline
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!
Floor 14 Posted 2006-12-01 06:39 ·  中国 江苏 南京 电信
高级用户
★★
Credits 623
Posts 214
Joined 2006-09-22 20:48
19-year member
UID 63387
Status Offline
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%
Floor 15 Posted 2006-12-01 06:53 ·  中国 上海 徐汇区 电信
初级用户
Credits 86
Posts 27
Joined 2005-09-25 13:58
20-year member
UID 42814
Status Offline
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....
Forum Jump: