Articles

Batch random number generator

April 11, 2009 at 13:21:27
Specs: Windows XP, 1024

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

Thanks
Brock


See More: Batch random number generator

Report •


#1
April 11, 2009 at 13:32:59

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.

Report •

#2
April 11, 2009 at 16:42:34

@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


Report •

#3
April 11, 2009 at 18:12:11

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.


Report •

Related Solutions

#4
April 11, 2009 at 19:28:03

%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%-99

Edit: comment+simplify+bugfix


Report •

#5
April 11, 2009 at 23:39:43

Judago,

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


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

M2


Report •

#6
April 12, 2009 at 00:46:22

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.


Report •

#7
April 12, 2009 at 00:52:25

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


Report •

#8
April 12, 2009 at 01:15:37

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

Report •

#9
April 12, 2009 at 01:48:22

any reason not using random mod 100 + 1 ?

Report •

#10
April 12, 2009 at 06:23:43

"any reason not using random mod 100 + 1 ?"

Subcategory: Batch


Report •

#11
April 12, 2009 at 07:24:10

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

Report •

#12
April 12, 2009 at 08:48:38

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.


Report •

#13
April 12, 2009 at 10:26:41

BatchFreak's answer was the same method I use. Works a charm!

Brockey01... you happy with the answers?


Report •

#14
April 12, 2009 at 15:34:04

how come this doesnt work?

this should generate 1 to 100 only but doesnt.


set myvar=%RANDOM%
set /a myvar*(101/32768)+1
pause


Report •

#15
April 12, 2009 at 19:32:49

thats a little bit of overkill lol

I only Batch if possible, 2000 more lines of code, oh well.


Report •


Ask Question