10316Views16Replies

# 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

10316Views16Replies

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

## Comments

Best Answer 11 years ago

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!

Answer 11 years ago

It does take a little bit.

But thank you so much!!

Answer 10 years ago

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

1 year ago

Using notepad I need tocreate an 8 Ball Guessing game. An 8 Ball game was a toythat when you shook it and looked at the bottom a message would appear. The game worked where you think of a question and shake the eight to get an answer by looking at the bottom of the eight ball (see picture).

11 years ago

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.)

Answer 11 years ago

Sorry but his works better

Answer 11 years ago

Did you try it?

Answer 11 years ago

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

Yours deserved a best answer!

Reply 1 year ago

You can use orksecurity's idea but remember the double percent sign. for example:

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

that worked for me

2 years ago

After reading the entire post out of a random search on the web I would like to help you all with the following command. "set /a var=%random% %% 9 + 5" All of you referring to this command have been using the following syntax as mentioned by Jasond51. "set /a var=%random% %% max + min" I'm going to just say it, this syntax is wrong. I admit I used this syntax quite a bit, but I got it from a different source which is not important here. After heavy experimentation I ran into what the first and second numbers really mean. Just try the following syntax...

"set /a var=%random% %% <range> + <starting number>"

When you act on incorrect incorrect information everybody is going to be confused and look for potentially odd alternatives.

5 years ago

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%

6 years ago

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..

7 years ago

:: 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

8 years ago

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?

Answer 7 years ago

random numbers easy

take the millseconds off %time% 0 to 99

then xor to get in your range

7 years ago

help

echo. space space space word space space word

echo word word

the spaces don't show why ?

how to cure it ?

10 years ago

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.

11 years ago

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.