How to generate random numbers in a specified range in bat

Source: Internet
Author: User

I recently studied batch processing commands and accidentally found that there are still random numbers. Before that, I did not dare to think about them.
When it comes to random numbers, different software has the function of random functions. The usage is similar, but there are differences.
This article describes how to use and use the batch processing command random in the MS-DOS.

1. random value range

To use random, you must use it as a variable to obtain the value.
Open the MS-DOS window and enter the following code to get the value.

Copy codeThe Code is as follows: echo % random %

% Random % can generate a random number between 0 and 65535.

2. Obtain random numbers within a certain range

Through 1, we know that % random % can generate a random number between 0 and 32767. But how can we get a random number within a certain range?
The general algorithm formula is as follows:
The general formula % random % (max-min + 1) + min is used to generate random numbers in the [min, max] range. Note: two % symbols are used to evaluate the modulo in batch processing.
For example, if we want to obtain a random number between 4 and 12, we can use the following code:

Copy codeThe Code is as follows: @ REM generates a random number between 10 [4, 12 ].
@ Echo off
REM enable latency environment variable extension
Setlocal enabledelayedexpansion
REM sets the minimum and maximum values of random numbers and the variables used for modulus.
Set min = 4
Set max = 12
Set/a mod =! Max! -! Min! + 1

For/l % I in (1, 1, 10) do (
REM generates a random number between [min, max ].
Set/a r =! Random! %! Mod! +! Min!
Echo.
Echo Random Number % I :! R!
)

Iii. Other instances

Example 1: Common functions
Generate a random number between 1 and 100

Copy codeThe Code is as follows: set/a RandomNumber = % random % 100 + 1
: 100 indicates the number of ranges. 1 indicates the base number, that is, the starting number. (used in bat). An error occurs in CMD.
: Random double digits.
Set/a d = % random :~ 0, 2%
Echo % d %
: Avoid 00
Set/a e = 1% random :~ 100 ,-
Echo % e % @ echo off
: Loop
Set/a RandomNumber = 1% random :~ -1% % random :~ -1%-99
: Maximum 100
If % RandomNumber % LSS 35 goto loop
: Less than 35
If % RandomNumber % GTR 75 goto loop
: Return value greater than 75
Echo % RandomNumber %
If "% RandomNumber %" = "50" pause
Goto loop

Instance ②: Randomly retrieve file names

Copy codeThe Code is as follows: @ echo off
If "% 1" NEQ "$ "(
For/f "tokens = 1, 2 delims =:" % a in ('"% ~ 0 "$ ^ | sort ') do @ echo % B
) Else for/f "delims =" % I in ('dir/B/a-d *. mp3') do @ call: sub % I
Goto: EOF: sub
Echo % random %: % *
Goto: EOF

Example 3: Randomly retrieve a row of data in a text

Copy codeThe Code is as follows: @ ECHO % DBG % OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR/F "TOKENS = *" % I IN (TEST. TXT) DO (SET/A h + = 1 & SET r! H! = % I)
SET/A s = % RANDOM % h % + 1
ECHO! R % s %!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.