Sunday, May 22, 2011

Generating a Sequence of Random Numbers in Batch Files

It's sometimes necessary to generate a sequence of random numbers in a script. In this blog you can find some posts on dealing with random numbers, still this question has never been discussed. Here is an example that shows how to generate and print on the console 99 random numbers. You can use our award-winning batch file editor to redirect the output to a text file and then build a plot in Excel to check these numbers being really random.
@echo off
setlocal
for /l %%i in (0,1,99) do call:rand %%i
for /f "tokens=3 delims=_=" %%i in ('set rand_') do echo %%i
goto:eof
:rand
set rand_%random%%random%=%1
This example can be useful for generating random data for different testing purposes. Hope you'll enjoy it.

No comments:

Post a Comment

Translate