A Lottery Batch file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
BatMaster
Posts: 28
Joined: 22 Dec 2010 12:53

A Lottery Batch file

#1 Post by BatMaster » 19 Aug 2011 15:25

Its basicly a uses the %random% command to generate a number between 1 and 49 six times,
the user inputs 6 numbers then the batch file compares them and adds the amount of matches to a variable.
i will release the code on the 25th of august.
--=BatMaster=--
p.s. i removed the poll
Last edited by BatMaster on 20 Aug 2011 10:45, edited 3 times in total.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: A Lottery Batch file

#2 Post by aGerman » 19 Aug 2011 16:13

Hi BatMaster,

for some reason polls don't work in this forum. I'm going to ask the forum owner why.
Writing a lottery code is an old exercise but a good exercise for learning comparisons.

Regards
aGerman

BatMaster
Posts: 28
Joined: 22 Dec 2010 12:53

Re: A Lottery Batch file

#3 Post by BatMaster » 19 Aug 2011 17:15

Thanks for the advice aGerman

BatMaster
Posts: 28
Joined: 22 Dec 2010 12:53

Re: A Lottery Batch file

#4 Post by BatMaster » 29 Aug 2011 04:29

as promised here is the code

Code: Select all

@echo off &setlocal
title TheLottery
set/a wins=0
:Start
cls
echo choose your numbers. 1-10 only!
set/p n1=1st:
set/p n2=2nd:
set/p n3=3rd:
set/p n4=4th:
set/p n5=5th:
set/p n6=6th:
:play1
if not "%n1%"=="%random% %% 10" goto play2
set/a wins="%wins% + 1"
:play2
if not "%n2%"=="%random% %% 10" goto play3
set/a wins="%wins% + 1"
:play3
if not "%n3%"=="%random% %% 10" goto play4
set/a wins="%wins% + 1"
:play4
if not "%n4%"=="%random% %% 10" goto play5
set/a wins="%wins% + 1"
:play5
if not "%n5%"=="%random% %% 10" goto play6
set/a wins="%wins% + 1"
:play6
if not "%n6%"=="%random% %% 10" goto draw
set/a wins="%wins% + 1"
:draw
cls
echo You got %wins% numbers right.
pause

jeb
Expert
Posts: 1042
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: A Lottery Batch file

#5 Post by jeb » 29 Aug 2011 05:21

Nice first shot, :)

but that's not a lottery program, as it requires also the correct order of the numbers.

So it's a difference if you take 1,2,3 or 3,2,1 as your numbers.

I would prefer "batch-arrays" like

Code: Select all

rem ** Get the numbers from the player
for /L %%n in (1,1,10) DO(
  set /p num[%%n]=%%nst:
)

rem ** The lottery numbers
for /L %%n in (1,1,10) DO(
  set /a lottery[%%n]=!random! %% 10
)

rem ** Search for each num[n] if it exists in lottery[] and if so add +1 to a correct counter
... (code is obvious)


jeb

Post Reply