How to make a random variable in ms-dos?

I need to make a random variable for my ms-dos game so it is different every time can you help?

This is pretty much the only thing i dont know because i need it between 1&3 so i cant find it except code that is very big and i cant understand it can you help?

Also i can give free tutors on google docs. :)

Update:

More: To change the between numbers do i change the *3 to what i want?

Comments

  • That depends on the language, doesn't it? If you mean "Windows command line" when you say "ms-dos", then you're probably writing a .BAT file and running with CMD.EXE. That's the enhanced command processor in Windows NT, 2000, and later. That has the set /a option:

    SET /A choice = %random% % 3 + 1

    That will create a variable "choice" between 1 and 3. The special environment variable %random% generates a random number between 0 and 32767 each time you use it. The set /a option sets a variable to the calculated value of an arithmetic expression. The % operator is remainder. A % B will give the remainder from dividing A by B (or B into A). That is always between 0 and B-1 when B is positive. The remainder %random% % 3 is between 0 and 2, then adding 1 shifts the range to 1 through 3.

    Warning: If you assign a value to the name "random" with "set random=..." then that fixed value overrides the random function, and you'll get no more random numbers for the rest of the batch file execution. Do it from the command prompt at the keyboard and you'll get no more randoms from that window for the rest of the session.

    You can find out more at the command prompt using either of these:

    set /?

    help set

    Edit: For more than three curtains, yes, just change the divisor:

    set /a choice = %random% % 42 + 1

    ... for a choice from 1 to 42

    set /a choice = %random% % (B + 1 - A) + A

    ... for a choice from A to B. By the way, the A and B can be variables that you have created, and no % signs are needed. However, %random% must be enclosed in %% always.

  • You can do Ms-Dos Batch command like (where the 3 is the number you change to change the end of range, and the 1 represents the number to change as your start of range):

    SET /A RANDNUM=%RANDOM% * 3 / 32768 + 1

    ... each time you do this the value of %RANDNUM% will be 1 or 2 or 3 randomly ... then you could make a call like ...

    myprog.exe %RANDNUM%

    ... to pass this as argv[1] to a C program, for instance ... depending on whether you have this in mind ... whereas if you mean to do the random number in something like C ... http://www.dbforums.com/delphi-c-etc/1208064-how-g... ... ?!

Sign In or Register to comment.