Sunday, May 1, 2011

Random Numbers Generator

As far as you probably know it is not so hard to generate random numbers with the help of batch files. But if you want to make sure that your number is unique among the given sequence of numbers, the task becomes a little bit more complicated. Here is a quite simple solution that can be modified with the help of our powerful batch files editor:


@echo off
setlocal
:try
set "rand=%random%%random%"
if "%rand:~6%"=="" goto try
1>nul 2>&1 type prevrand.txt | find "%rand:~0,6%"
if not errorlevel 1 goto try
echo %rand:~0,6%>prevrand.txt




As far as you can see, we use a special text file to store all previously used numbers. Of course, it's better to remove it after you have done all the work, whatever you need to do with the generated numbers. Hope you'll find this small random numbers generator useful.

No comments:

Post a Comment

Translate