87Views4Replies

Author Options:

How do you make a batch file that only chooses 1-4? Answered

I really need a batch file that picks a random number from 1-4. My friends and I sometimes choose straws when we are doing things, I just thought that this would be faster and more efficient. Also I need to know how to get it so that it picks a number 90% of the time (Its a joke with my friends, so that one of my friends has to do it the most.)

Discussions

0
None
g-one

Best Answer 3 years ago

It's a one-liner in Batch:

@for /f %%i in ('"set /a %random% %% 4 + 1"') do @set /p =%%i

Another one-liner for your joke:

@for /f %%i in ('"set /a %random% %% 40 + 1"') do @if %%i gtr 4 (set /p =2) else set /p =%%i

No need for the usual @echo off. Hit Enter to close the window.

If you want to pick a number other than 2 90% of the time then replace the 2 in the first SET /P clause with 1, 3, or 4.

0
None
rickharris

3 years ago

Very little is faster than a simple mechanical system. Make a spinner with 4 numbers on it.

spinner.jpg
0
None
iceng

3 years ago

Simply pick a random number 1 through 40 and translate every number over four to 4 ... ie 5=>4 & 6=>4 --- 39=>4 & 40=>4..