Active7 months ago
I have a batch script that generates random numbers from 1 to 10000.
Generate a Random Number for a PIN. To generate a 4 digit PIN without duplicate digits, choose Min = 0, Max = 9 and Generate 4 Numbers. Digit numbers. Random Numbers. Possible 4 digit combinations 10,000 ( 10.0k). Random 2 Digit Number Generator Pick Random Numbers.
My Batch Script
Current Outputs
Expected Outputs
Question
I need the random number to be consisted of 4 digits only and not 1, 2, or 3 digits and always 4.
Hennes60k77 gold badges9494 silver badges143143 bronze badges
PhilipPhilip
4 Answers
Generate Random 4-Digit Number Windows Batch Script
You can use a batch script with Windows to complete this task easily as I'll explain below.
Essentially this will. . .
- Use PowerShell'sGet-Random function to generate the random number
- The random number value will be in a range of
0000-9999
using the-Maximum
and-Minimum
parameters
- The random number value will be in a range of
- Use the '0' Custom Specifier—'{0:0000} -f $fRandom' with PowerShell and .NET to ensure the random number value has leading zeros added ensuring it always has 4 digits
- Result in the PowerShell random number value being
echo
'd as the output - Use a FOR /F loop to take the result of the executed PowerShell script (the random number) within it, and save that result as a variable to use in the batch script for your needs (
%RAND%
)
Batch Script
If the random number cannot be 4-digit values with prefixed zeros like
0001
, 0011
, or 0111
you can change the -Minimum 1000
like that instead but random digits 1-999
will be omitted so the value of 1000
is what you want to use for the -Minimum
parameter if that's the case.Further Resources
Pimp Juice ITPimp Juice IT27.3k1111 gold badges4343 silver badges8282 bronze badges
A simple solution could be to take the last digit of four random generated numbers
MC NDMC ND
You just need to adjust the minimum value and the range on your initial solution.
acegsacegs
Your could use my batch script:
Then drag in the C:WindowsSystem32 Folder and type 'random /?' for the syntax.
HayzHayz