Batch File: Random number

Hi there,

I am to tired to script so I need help.

I need to make this batch file to display 50 random numbers between 1 and 1000.

Thanks,
Rene

@echo off
FOR /L %%A IN (100,1,150) DO SET /a Val.%%A=%random%%%1000+1
FOR /F "tokens=2 delims==" %%A IN ('SET Val.') DO ECHO %%A
PAUSE

Open in new window

LVL 10
ReneGeAsked:
Who is Participating?
 
oBdACommented:
You need delayed expansion for this:
@echo off
setlocal enabledelayedexpansion
FOR /L %%A IN (100,1,150) DO SET /a Val.%%A = !random! %% 1000 + 1
FOR /F "tokens=2 delims==" %%A IN ('SET Val.') DO ECHO %%A
PAUSE

Open in new window

0
 
ReneGeAuthor Commented:
Thanks oBdA
Will give some points to netcmh for his/her trying.


Thanks and cheers,
Rene
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
netcmhCommented:
Thanks! I appreciate it. Good luck
0
 
Paul TomasiCommented:
oBdA

>> "FOR /L %%A IN (100,1,150) DO SET /a Val.%%A = !random! %% 1000 + 1"

Why didn't you go from '1' to '50'?

A possible problem with random numbers in FOR-loops is that somethimes the same number is given when generated in close succession to each other.

For this reason, and when totally unique numbers are required then, writing the numbers out to a text file is helpful when used with FINDSTR /B as in the following:
copy /y nul random.txt >nul
set count=0

:loop
   set /a rnd=%random% %% 1000 +1
   findstr /b "%rnd%" random.txt

   if %errorlevel% equ 1 (
      echo %rnd% >> random.txt
      set /a count+=1
   )

if %count% lss 50 goto :loop

for /f %%a in (random.txt) do echo %%a
pause

Open in new window


Or something along those lines.






0
 
ReneGeAuthor Commented:
@oBdA
I was just trying to figure this, but my brains hit a wall (I slept 2 hours last night).

Here is my final script. Would you mind looking at it and see that if a winner is drawned again, it skips it and continue to add new winners until the desired quantity of winners have been reached.

%1 in a list of emails in a text file.

Thanks a million
0
 
ReneGeAuthor Commented:

@echo off
setlocal enabledelayedexpansion
IF "%~1" EQU "" (
	ECHO PLEASE DRAG AND DROP YOUR EMAIL LIST FILE.
	ECHO IN A TEXT FORMAT ".txt" NOT EXCEL FORMAT.
	PAUSE
	EXIT
)

IF "%~x1" NEQ ".txt" (
	ECHO YOUR LIST FILE MUST BE IN A TEXT FORMAT ".txt".
	PAUSE
	EXIT
)

SET Output=%~dpn1_GAGNANTS%~x1

ECHO Please enter the number of desired winners
SET /P WinnersNumber=

SET Count=0
FOR /F "usebackq delims=" %%A IN ("%~1") DO SET /a Count+=1
ECHO There are [%Count%] email(s) and [%WinnersNumber%] winners to be.
ECHO.

FOR /L %%G IN (1,1,%WinnersNumber%) DO (
	ECHO Draw Number:%%G
	PING 127.0.0.1 -n 1 >NUL
	FOR /L %%A IN (1,1,50) DO (
		SET /a Val = !random! %% %Count% + 1
		TITLE !Val!
		PING 127.0.0.1 -n 1 >NUL
	)
	CALL :Draw "%~1"
	ECHO [!TheWinnerIs!]
	ECHO %date%,%time%,!TheWinnerIs!>>"%Output%"
	ECHO.
)

PAUSE
EXIT

:Draw
SET /a ValSkip=%Val% - 1
FOR /F "usebackq skip=%ValSkip% delims=" %%A IN ("%~1") DO (
	SET TheWinnerIs=%%A
	exit /B
)

Open in new window

0
 
ReneGeAuthor Commented:
Oups, sorry paul :(

I did not realised it was you that commented.

If you feel that fixing my script may require lots a tweaks, let me know I'll create a new question.

Cheers from brain dead scripter!!
0
 
ReneGeAuthor Commented:
Here is the new question for my script:
http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_27401447.html

@paul: I owe you some points here! Sorry for that!

Thanks and cheers,
Rene
0
 
Paul TomasiCommented:
ReneGe

Not at all. I didn't know it was you either until I just looked...

All I saw was that you needed to display 50 random numbers between 1 and 1000.

I guessed the problem might have something to do with the computer not being able to generate random numbers quick enough between requests.

I'm not sure if that was the original problem or not...

Out of curiosity, what is it you're trying to do?....

0
 
Paul TomasiCommented:
Ah, right! I understand it now (as explained in the link)... lol
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.