Checking if 3 random numbers are same

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
silent
Posts: 44
Joined: 28 Oct 2011 14:40

Checking if 3 random numbers are same

#1 Post by silent » 09 Feb 2012 14:43

Hi,do you know a game called Jackpot ? i think most of people know whats it.I know how to make a random number generator etc,but i want to know how to check if those 3 numbers are same,so if it will put 3 3 3 you will win.I thought about IF %random1% eq %random2% (im not sure about eq,i know there was a command like that but i forgot how it was called :D,eq or equ,i mean about equal)

Ocalabob
Posts: 79
Joined: 24 Dec 2010 12:16
Location: Micanopy Florida

Re: Checking if 3 random numbers are same

#2 Post by Ocalabob » 09 Feb 2012 15:53

Greetings silent,

I am not familiar with Jackpot but maybe this will help.

Type if /? at a command prompt and you will find;

EQU - equal
NEQ - not equal
LSS - less than
LEQ - less than or equal
GTR - greater than
GEQ - greater than or equal

Best wishes.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Checking if 3 random numbers are same

#3 Post by Squashman » 09 Feb 2012 17:09

In this instance all you really need to do is compare it as as string. You don't need to use the extended compare operators.

Code: Select all

 IF "%R1%"=="%R2%" (
    IF "%R1%"=="%R3%" SET VAR=YES
)



silent wrote:(im not sure about eq,i know there was a command like that but i forgot how it was called :D,eq or equ,i mean about equal)

Then why don't you look at the help for the IF command.

Aacini
Expert
Posts: 1930
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Checking if 3 random numbers are same

#4 Post by Aacini » 09 Feb 2012 20:53

You need to combine an IF command inside another one to assemble an AND operator, that is, IF R1 == R2 IF R1 == R3 ... I suggest you to made this point clear by creating an auxiliary variable called AND this way:

Code: Select all

SET AND=IF
IF %R1% == %R2% %AND% %R1% == %R3% ECHO YOU WIN

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Checking if 3 random numbers are same

#5 Post by Liviu » 09 Feb 2012 21:35

Aacini wrote:I suggest you to made this point clear by creating an auxiliary variable called AND this way:

Code: Select all

SET AND=IF
IF %R1% == %R2% %AND% %R1% == %R3% ECHO YOU WIN

I wouldn't find that too clear in general. It could easily mislead one to write...

Code: Select all

IF %R1% == %R2% %AND% %R1% == %R3% (ECHO YOU WIN) ELSE (ECHO YOU LOSE)
...with the expectation that it always echoes something.

Liviu

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Checking if 3 random numbers are same

#6 Post by dbenham » 09 Feb 2012 23:49

Liviu has a good point :!:

----------------------------------

Using a bit of math, it is possible to roll the dice and get the result with a single SET statement. Then a simple IF statement prints out the result.

Code: Select all

@echo off
setlocal enableDelayedExpansion
set /a "r1=!random!%%10, r2=!random!%%10, r3=!random!%%10, result=^!(r1-r2)&^!(r1-r3)"
if !result!==1 (echo WINNER^^^!) else echo You lose



Dave Benham

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

Re: Checking if 3 random numbers are same

#7 Post by jeb » 10 Feb 2012 01:47

Even without math you could create a single IF

Code: Select all

@echo off
set /a r1=%random% %% 10
set /a r2=%random% %% 10
set /a r3=%random% %% 10
echo "%r1%,%r2%,%r3%"
if "%r1%,%r2%,%r3%"=="%r2%,%r3%,%r1%" echo All same


jeb

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Checking if 3 random numbers are same

#8 Post by Squashman » 10 Feb 2012 06:52

jeb wrote:Even without math you could create a single IF

Code: Select all

@echo off
set /a r1=%random% %% 10
set /a r2=%random% %% 10
set /a r3=%random% %% 10
echo "%r1%,%r2%,%r3%"
if "%r1%,%r2%,%r3%"=="%r2%,%r3%,%r1%" echo All same


jeb

Nice jeb. This just shows how we sometimes over complicate things and again you show us how to keep it simple.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Checking if 3 random numbers are same

#9 Post by dbenham » 10 Feb 2012 07:38

@jeb - I like the way you think :)

It took me a moment to notice the string rotation in the comparison. As long as we are restricted to single digit numbers, then the commas are not needed. Two digits or more and the commas are definitely needed.


Dave Benham

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Checking if 3 random numbers are same

#10 Post by Liviu » 10 Feb 2012 11:35

jeb wrote:

Code: Select all

if "%r1%,%r2%,%r3%"=="%r2%,%r3%,%r1%" echo All same

Nice! In the name of readability^H^H terseness I submit this works as well ;-)

Code: Select all

if "%r1%,%r2%"=="%r2%,%r3%" echo All same

Liviu

Post Reply