8171Views14Replies

# Batch Help(randomization)?

Hello Im workin on a batch file and i would like to have help with with making Cmd randomize between two selected numbers Example: Random 1-10 Answer: 3

8171Views14Replies

Hello Im workin on a batch file and i would like to have help with with making Cmd randomize between two selected numbers Example: Random 1-10 Answer: 3

Chromatica, There is a way to do that! Without that process down there.

So you want 1-10 eh? You can do this by forcing batch to pick a number between 1-10, type this on text and save as .bat and incorporate it into your stuff.

Ok follow me on this! It may take around 1 second to 1 minute to generate the number because there are over 32000 possible answers so be patient! I tested that it works!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

@echo off

:ans

set /a ran=%random%

if /i %ran% GTR 10 goto :ans

if /i %ran% LSS 1 goto :ans

echo Batch chose the number %ran%

pause

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

As you can see GTR and LSS mean that if it is greater than 10 or lower than 1 it will regenerate the number until it is between those limits! IPlease read this!

Select as Best AnswerUndo Best Answer

It does take a little bit.

But thank you so much!!

Select as Best AnswerUndo Best Answer

One time i made a guessing game from that and it was 1-20, but it took about 3 min. to fine a number

Select as Best AnswerUndo Best Answer

OK so batch is not great at random numbers

the best and easiest way in my opinion is to do this set /a var=%random% %% max + min

however if you try to set your min to something greater than zero it will ignore the maximum number

to avoid this you can put it in a loop like this

:start

set /a var=%random% %% 9 + 5

if %var% gtr 9 go to start

echo %var%

Select as Best AnswerUndo Best Answer

So I used the set /a number=%random% %% 40 + 5 but basically if you don't use 1 as the min number the number will go beyond the max for example using Min1 Max5 does from 1 to 5 but using min5 max10 it goes like 7,9 then crosses the max number... The language of BAT isn't the best..

Select as Best AnswerUndo Best Answer

:: pick a random number between say 0 to 19 or 1 to 20

:: take the hundredths of a second 0 to 99 from current time

set eddie=%time:~-2%

:: modulus doesn't like numbers like 08, it thinks it's octal, so knock off the zero

set first=%eddie:~0,1%

if %first%==0 set eddie=%eddie:~1,1%

:: make number modulus 20 to keep range 0 to 19 , so 99 mod 20 = 19

:: if you want 1 to 20 change the modulus to 19 and add 1 to eddie

set /a eddie=(%eddie%%%20)

echo number=%eddie%

pause

Select as Best AnswerUndo Best Answer

Guys, seriously. orksecurity has a much better solution. It doesn't take a million years.

set /a number=%random% % max + min

The reason it doesn't work in batch files is because of a simple over-look.

set /a number=%random% %% max + min

Or have you all forgot that in the for command you need double percent signs instead of singles?

Select as Best AnswerUndo Best Answer

random numbers easy

take the millseconds off %time% 0 to 99

then xor to get in your range

Select as Best AnswerUndo Best Answer

help

echo. space space space word space space word

echo word word

the spaces don't show why ?

how to cure it ?

Select as Best AnswerUndo Best Answer

set /a num=%random% / (32767 / %yourmaxnum%)

will pick a random number between 0 and %youmaxnum% (replace %yourmaxnum% with a number like 10, or else

set/a yourmaxnum=10

)

Thank you, Padlock.

Select as Best AnswerUndo Best Answer

You can do what knex_mepalm did, but that takes _forever_ to load. Not to offend him, or anybody, but, uh... There are no good ways, the most commonly used method is knex_mepalm's.

Select as Best AnswerUndo Best Answer

From "help set":

%RANDOM% - expands to a random decimal number between 0 and 32767.

To get a number within a more limited range, use the remainder operation:

set /a a=%random% % 10 + 1

will set %a% to a value from 1 to 10. This will not be perfectly distributed since 10 is not an even factor of 32768, but it should be close enough for anything you're doing in a batch file. If you need more sophistication, learn a real programming language.

("help cmd", and help on the other CMD commands, is also worth checking when trying to understand what batch files can and can't do.)

Select as Best AnswerUndo Best Answer

Sorry but his works better

Select as Best AnswerUndo Best Answer

Did you try it?

Select as Best AnswerUndo Best Answer

I tried both and his didn't come up with anything.

Yours deserved a best answer!

Select as Best AnswerUndo Best Answer