Showing posts with label random numbers. Show all posts
Showing posts with label random numbers. Show all posts

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.

Friday, May 13, 2011

A Small Quiz Script :)

There are lots of useful scripts already published on this blog. You can create even more of them with the help of Dr.Batcher. Now it's time for jokes. Let us introduce a small quiz script that can be useful to check whether you are lucky today. Hope you'll enjoy it:)
@echo off
set i=0
echo Thinking of a number...
:rand
set nmb=%random%
if %
nmb% GTR 10 goto rand
if %
nmb% EQU 0 goto rand
echo Now you can try 3 times to guess the right one!
:enter
set /p "ent=Your number : "
set /a i=%i%+1
if %ent% EQU %
nmb% (
echo You're lucky!
pause
exit
)
if %i% LSS 3 (
goto enter
)
if %i% EQU 3 (
echo Game Over:(
pause
)

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.

Thursday, February 3, 2011

Retrieving Random String from Batch File

The task to be discussed in this post is retrieving a random string from the given text file and then printing it on the screen. When is it useful? For example, you can create the list of user's addresses and send someone a message via 'net send' command as a simple joke. Or even automate the creation of SEO texts with the help of batch files. Enjoy the code:
@echo off
setlocal
set "lines=0"
for /f "tokens=*" %%a in (items.txt) do set /a "lines+=1"
set /a "skip=%random% %% lines"
if %skip% lss 1 (set "skip=") else (set "skip=skip=%skip%")
for /f "%skip% tokens=*" %%a in (items.txt) do set "item=%%a"&goto display
:display
echo %item%
Hope it'll be useful, or at least just amazing for you.

Translate