[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Batch File: Random number

Posted on 2011-10-17
11
Medium Priority
?
638 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:ReneGe
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 21

Assisted Solution

by:netcmh
netcmh earned 80 total points
ID: 36981110
0
 
LVL 85

Accepted Solution

by:
oBdA earned 1920 total points
ID: 36981159
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
 
LVL 10

Author Comment

by:ReneGe
ID: 36981262
Thanks oBdA
Will give some points to netcmh for his/her trying.


Thanks and cheers,
Rene
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 21

Expert Comment

by:netcmh
ID: 36981457
Thanks! I appreciate it. Good luck
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 36981576
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
 
LVL 10

Author Comment

by:ReneGe
ID: 36981744
@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
 
LVL 10

Author Comment

by:ReneGe
ID: 36981752

@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
 
LVL 10

Author Comment

by:ReneGe
ID: 36981933
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
 
LVL 10

Author Comment

by:ReneGe
ID: 36983003
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
 
LVL 11

Expert Comment

by:paultomasi
ID: 36983279
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
 
LVL 11

Expert Comment

by:paultomasi
ID: 36983282
Ah, right! I understand it now (as explained in the link)... lol
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Suggested Courses
Course of the Month20 days, 1 hour left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question