"Offering free computer help, hints, and tips to the Internet populace." Now with over 3,820 tips!

Windows 2000 and DOS

Simulate a Dice Roll

SUMMARY: Simulate a dice roll with Windows 2000 and DOS. This article provides tips on the expanded 'set' command as well as how to use the %random% environment variable.


 

Windows 2000's DOS includes an environmental variable called %random%. This variable contains a random number from 0-32767. While this is useful in automatically generating temporary file names for file manipulation, let's do something really useful with this variable. Take this random number, perform some arithmetic, and get a command to simulate a random dice roll.

Start by setting the environment variable %dice% to be the result of this random number. The first command will create the 'dice' variable; the second merely displays the results.

C:\>set dice=%random%
C:\>echo %dice%
12663

Unless you have a VERY large dice with many, many sides, you're not likely to roll this number. So, let's manipulate it a little bit.

With the /a parameter to the SET command, we can perform arithmetic. We only want numbers 1-6. A common technique to get smaller numbers from a random larger number is to divide the larger number by the smaller number, taking the remainder. Let's illustrate by assuming we get random numbers seven through twelve to appear. Then:

7 divided by 6 = 1 with a remainder of 1.
8 divided by 6 = 1 with a remainder of 2.
...
11 divided by 6 = 1 with a remainder of 5.
12 divided by 6 = 1 with a remainder of 0 (more on this later).

So, at the DOS prompt, divide the retrieved random number by 6 and get its remainder. For this, we use the modules (%) parameter.

C:\>set /a dice=%random% % 6
5
C:\>echo %dice%
5
C:\>set /a dice=%random% % 6
1
C:\>echo %dice%
1

Do this enough, however, and you'll see a 0. This is because the random number can be equally divisible by 6, giving a 0 remainder. For example, if the random number is 12, as shown above, then 12 divided by 6 = 2, remainder 0.

A dice should not roll 0, so add one to the total to ensure the numbers come out from 1 through 6, not from 0 through 5:

C:\>set /a dice=%random% % 6 + 1
4
C:\>echo %dice%
4

You'll notice the first command always outputs the result of the arithmetic. We only want to see the dice roll result when we echo the %dice% variable ourselves, so get rid of the automatic output of the "set /a" command.

C:\>set /a dice=%random% % 6 + 1 > nul
C:\>echo %dice%
2

There! Now we have a command to simulate a dice roll and place the results in environment variable %dice%. And, you've learned a lot about batch files in the process.

If someone wants to take this and create a complete-working dice game in the Windows 2000 DOS prompt, go for it! Let me know, and I just might show your results in a future MalekTip.



 

Got something to add? Post a comment!

Everyone is invited to submit comments. Registered users also receive their own visitor profile page with a summary of recent posts.
Register | Login

Your name:
 E-mail:
* Home/Blog Page:
Comment:
(Max chars: 5000)

* = optional field

 


Print This Tip

Get E-Mail When New Tips are Online

Return to the Windows 2000 and DOS page.

 


New in MalekTips:

RSS Feeds- Subscribe!

You want the latest tech tips and tricks in your e-mail Inbox - FREE? Type your e-mail address below and click 'Get Tips!'.
 


( sample / details )
( opt-out instructions )