Help?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dedlygaimer
Posts: 2
Joined: 01 May 2012 17:36

Help?

#1 Post by dedlygaimer » 01 May 2012 17:40

I'm in a bit of a predicament.

I'm trying to get a code that will determine if a random integer is even or odd, and depending on what the answer is perform from two different actions each time the script is referred to.

Here's my code:


set /a compnumber=%random%

set /a modulo = “%compnumber% %% 2?
if %modulo% == 0 (
rem echo EVEN
) else (
rem echo ODD
)
pause

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Help?

#2 Post by Fawers » 01 May 2012 17:52

dedlygaimer wrote:set /a compnumber=%random%

set /a modulo="%compnumber% %% 2"
if %modulo% == 0 (
rem echo EVEN
) else (
rem echo ODD
)
pause

dedlygaimer
Posts: 2
Joined: 01 May 2012 17:36

Re: Help?

#3 Post by dedlygaimer » 01 May 2012 18:04

Did you even change anything? :P

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

Re: Help?

#4 Post by Squashman » 01 May 2012 19:10

dedlygaimer wrote:Did you even change anything? :P

I see at least 3 changes to the code.

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

Re: Help?

#5 Post by Aacini » 01 May 2012 20:54

Code: Select all

set /a compnumber=%random%

set /a "modulo=compnumber %% 2"
if %modulo% == 0 (
rem echo EVEN
) else (
rem echo ODD
)
pause

neorobin
Posts: 47
Joined: 01 May 2012 12:18

Re: Help?

#6 Post by neorobin » 01 May 2012 21:37

Another method to determine the parity is to make the number and 1 to do a BITAND operation.

Code: Select all

set /a compnumber=%random%

set /a "modulo = compnumber & 1"
if %modulo% == 0 (
rem echo EVEN
) else (
rem echo ODD
)
pause

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help?

#7 Post by foxidrive » 02 May 2012 01:45

Another method:


Code: Select all

@echo off
set num=%1
set num=%num:~-1%
for /f "delims=13579" %%a in ("%num%a") do if "%%a"=="a" (echo odd) else (echo even)
pause

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help?

#8 Post by foxidrive » 02 May 2012 01:49

Squashman wrote:
dedlygaimer wrote:Did you even change anything? :P

I see at least 3 changes to the code.


I see one change. Must be my old eyes. ;)

neorobin
Posts: 47
Joined: 01 May 2012 12:18

Re: Help?

#9 Post by neorobin » 02 May 2012 02:59

foxidrive wrote:
Squashman wrote:
dedlygaimer wrote:Did you even change anything? :P

I see at least 3 changes to the code.


I see one change. Must be my old eyes. ;)

I also see only one change.

Squashman means to remove the 2 spaces?

set /a modulo_=_"%compnumber% %% 2"

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

Re: Help?

#10 Post by Squashman » 02 May 2012 06:08

foxidrive wrote:
Squashman wrote:
dedlygaimer wrote:Did you even change anything? :P

I see at least 3 changes to the code.


I see one change. Must be my old eyes. ;)

Removed the spaces.
Added the correct quotes.
Removed the question mark.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help?

#11 Post by foxidrive » 02 May 2012 08:33

Squashman wrote:
foxidrive wrote:I see one change. Must be my old eyes. ;)

Removed the spaces.
Added the correct quotes.
Removed the question mark.


Ahh, I told you it was my eyes. :)

ta.

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Help?

#12 Post by Fawers » 02 May 2012 09:29

At least we have more than 2 versions to rely on. But still,
Squashman wrote:Removed the spaces.
Added the correct quotes.
Removed the question mark.

Thank you very much.

Post Reply