# Solved: Batch Dice Roller...



## Codiah (Sep 27, 2008)

Hey everyone, kinda of stuck with this little idea..

I'm wanting to make a batch file that will return a value at random from 1 - 6, just like a six sided die...

not entirely sure how to go aboutdoing this, i thought maybe pulling out a section of the %random% variable, and then checking aginst it to make sure its a number in the feild of 1 - 6, but i was wondering if there was a little less intensive way to do it...

any input would be great...

and i'll post my idea here... just want to know if anyone has a better solution, but it has to be in batch scripting.. its for a personal project of mine.. and don't get me wrong, this works very well, but i'm just curious if there another way...


```
@ECHO off
TITLE DICE ROLL
::
:: PRODUCES A RANDOM NUMBER BETWEEN 1 AND 6 ::
:: I USED NUMBERS FROM THE MIDDLE OF THE RANDOME ::
:: ARIABLE BECAUSE THEY SEEM TO HAVE A BETTER 
:: PROBABLITY OF BEING DIFFERENT WITH EACH REROLL ::
::
:RE_ROLL
set DICE_ROLL=%RANDOM:~3,4%
::
IF '%DICE_ROLL%'== '1' GOTO SHOWRESULT
IF '%DICE_ROLL%'== '2' GOTO SHOWRESULT
IF '%DICE_ROLL%'== '3' GOTO SHOWRESULT
IF '%DICE_ROLL%'== '4' GOTO SHOWRESULT
IF '%DICE_ROLL%'== '5' GOTO SHOWRESULT
IF '%DICE_ROLL%'== '6' GOTO SHOWRESULT
GOTO RE_ROLL
::
:SHOWRESULT
ECHO.
ECHO.
ECHO You rolled a "%DICE_ROLL%"
ECHO.
ECHO Push any key to roll again.
PAUSE >nul
CLS
GOTO RE_ROLL
```


----------



## TheOutcaste (Aug 8, 2007)

Codiah said:


> set DICE_ROLL=%RANDOM:~3,4%


As %random% never returns a number over 32767, the length should be 2 as it will never be larger than 5 digits; anything higher is ignored.
Starting as position 3 means you are rejecting any %random% value less than 1001 and greater than 9996 so you'll have a lot of re-rolling.

What I would use is this:
*Set /A DICE_ROLL="%RANDOM% % 6"+1*

What this does is gives you *%RANDOM% modulus 6*, or in other words divides %RANDOM% by 6 and returns the remainder, which will always be 0-5. Then add 1 to get 1-6. This way you don't need all the If statements.
You can use this for rolling multi-sided die, like 8 or 12 sided by changing the modulus number to match the number of sides.

The quotes are required around the modulus operands.

HTH

Jerry


----------



## Codiah (Sep 27, 2008)

Well once again i have to say thank you, i knew there was a simpler and more efficient way to accomplish this task, so thanks for the response... 

-Codiah


----------



## Codiah (Sep 27, 2008)

Well, i'm back and i regret to inform you that this in't working out, perhaps i'm doing something wrong...

When I use the idea above in a batch file, i get the output "Missing Operator"

But if i just execute the command in the Command Shell it works exactly as you said it would...


----------



## Codiah (Sep 27, 2008)

Opps, just dawned on me, i have to double up the % sign for it to work correctly in a batch script.... 

my mistake...

My script now runs much smoother, and it seems to produce random numbers much better, i get allot less doubles than before....

Appreciateit..


----------



## TheOutcaste (Aug 8, 2007)

Aha, see ya found it. I thought the quotes would mean you didn't have to double up the Modulus symbol (%) and was why they were required, but ya do. Obviously I didn't test it in a batch, just at the command line.

Jerry


----------



## Codiah (Sep 27, 2008)

Just figured i'd show you my finished project for the heck of it... it a simple script that allows you to roll from 1 to 6 dice, and then displays them using simple Ascii Graphics i threw together, and thanks to you it works much smoother now...

here's a screenshot of rolling all 6 dice..










*here is the script i use...*


```
@ECHO off
mode con cols=15 lines=50
COLOR 80
TITLE DICE ROLLER
SET DICEIMGPATH=DICE_IMAGES
::
:DICE_START
CLS
COLOR 80
ECHO.
ECHO.
ECHO How many Dice
ECHO would you like
ECHO to roll?
ECHO.
ECHO Pick a number
ECHO from 1-6...
ECHO.
SET /p DICE_NUMBER=Number:
IF '%DICE_NUMBER%'== '1' GOTO START_DICE_ROLL
IF '%DICE_NUMBER%'== '2' GOTO START_DICE_ROLL
IF '%DICE_NUMBER%'== '3' GOTO START_DICE_ROLL
IF '%DICE_NUMBER%'== '4' GOTO START_DICE_ROLL
IF '%DICE_NUMBER%'== '5' GOTO START_DICE_ROLL
IF '%DICE_NUMBER%'== '6' GOTO START_DICE_ROLL
::
GOTO DICE_ERROR
::
:DICE_ERROR
CLS
COLOR 84
ECHO.
ECHO.
ECHO ::::::::::::::
ECHO ::::::::::::::
ECHO.
ECHO Invalid number 
ECHO   Pick Again
ECHO.
ECHO ::::::::::::::
ECHO ::::::::::::::
PING localhost -n 4 >nul
GOTO DICE_START
::
:START_DICE_ROLL
CLS
set max=%DICE_NUMBER%
set count=0
:RE_ROLL
::
Set /A DICE_ROLL="%RANDOM% %% 6"+1
::
ECHO.
TYPE %DICEIMGPATH%\DIE%DICE_ROLL%.ASC
set /a count=%count%+1
if not %count%==%max% GOTO RE_ROLL
GOTO END_ROLL
::
:END_ROLL
set count=0
ECHO.
ECHO.
ECHO.
ECHO.
ECHO  Push any key
ECHO  to roll again
PAUSE >nul
CLS
GOTO RE_ROLL
```


----------



## TheOutcaste (Aug 8, 2007)

Looks pretty good, but where the cute cocktail waitress in the skimpy costume?

This might be a bit more efficient at checking for a valid number:


```
SET /p DICE_NUMBER=Number:
IF '%DICE_NUMBER%' LSS '1' GOTO DICE_ERROR
IF '%DICE_NUMBER%' LSS '7' GOTO START_DICE_ROLL
:DICE_ERROR
```
Jerry


----------



## TheOutcaste (Aug 8, 2007)

Should point out that using LSS like that only works for 1 digit numbers as it's actually doing a string compare. So "2" is NOT less than "12" for example.

Jerry


----------



## Codiah (Sep 27, 2008)

Oh wonderful, learning something new all the time, i've been searching for a good read on batch Scripting, do you have any suggestions, right now i'm working off of collected knowledge gained through forums and discussion... most things i've found only discribe the basic commands and their basic arguments, i'm looking for something a little more in depth...


----------



## TheOutcaste (Aug 8, 2007)

DOS and VB Scripting Links
Command-line reference A-Z
Using batch parameters
Windows 95/98/ME Batch file Tutorial (Still a good basic reference for WinNT/2K/XP)
Batch File Functions for NT4/2000/XP/2003
Rob van der Woude's Scripting Pages

There's a start. The built in help is also a good source. Getting to know *For*, *IF*, and *Set* in particular are a big help. Best way I've found to learn them is helping people here, trying to figure out ways to do what they want.

Just be very very careful when combining For loops and DEL or RD; I always use Echo to make sure of just what I'm deleting, or test it in a virtual machine

Jerry


----------

