I would like to know if it is possible to make a random number generator in batch from 1 to 100? Thanks

Brock

I forgot i need to be automatic with no user intervention if possiable. i tried to make on going off the first digit of the internal clock time.

@echo off & setLocal EnableDelayedExpansion for /L %%a in (1 1 100) do (

echo !random!

)

=====================================

If at first you don't succeed, you're about average.M2

Thanks for that. how could i generate a number from 1 to 100 that seems to generate number alot alarger. but non less thank you for work.

%random:~-2% or %random:~0,2% will generate a number 0-99. The later won't have leading zero's but may sometimes be skewed. <untested>

::Random number 1 - 100 inclusive

set ran=%random:~-2%

if "%ran:~1%"=="" set ran=0%ran%

set /a ran=1%ran%-99Edit: comment+simplify+bugfix

Judago, You're quite right about %random:~0,2% but why?

=====================================

If at first you don't succeed, you're about average.M2

M2, I'm not sure if you are refering to the leading zero's or why the other way is skewed, so I'll address both.

As you would know %random% returns a number 0-32767. Substrings still apply on %random% but they will only be a part of the full number that would be returned, so if random returns 101 and %random:~-2% is used we will end up with 01 - bearing in mind that if it had of returned only 1 the leading zero will not be present.

As for being skewed I was refering to how more numbers will start with 1,2 and 3 (because of 32767 being the last available number) than 4, 5, 6, 7, 8 and 9.

Hi Judago, Thanks. I get the skew, but this leading 0 is going to bug me for a while.

best regards

=====================================

If at first you don't succeed, you're about average.M2

Don't let it bug you, the people responsible for this might be able to tell us when these zero's are coming...;)

any reason not using random mod 100 + 1 ?

"any reason not using random mod 100 + 1 ?"Subcategory: Batch

judago, i might be wrong but the below batch code works well in my pc:

C:\>set/a %random% % 100 +1

69

C:\>set/a %random% % 100 +1

86

C:\>set/a %random% % 100 +1

34

heres one that Ive ude before, not as efficient, but it works. :beg

set rnd %random%

if %rnd% GTR 100 goto beg

echo The number is %rnd%I only Batch if possible, 2000 more lines of code, oh well.

BatchFreak's answer was the same method I use. Works a charm! Brockey01... you happy with the answers?

how come this doesnt work? this should generate 1 to 100 only but doesnt.

set myvar=%RANDOM%

set /a myvar*(101/32768)+1

pause

thats a little bit of overkill lol I only Batch if possible, 2000 more lines of code, oh well.

Ask Your Question

Weekly Poll

Do you think police officers should wear body cameras?

Discuss in The Lounge

Poll History