Computing.Net > Forums > Programming > Batch random number generator

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch random number generator

Reply to Message Icon

Name: brockey01
Date: April 11, 2009 at 13:21:27 Pacific
OS: Windows XP
CPU/Ram: 1024
Product: Hewlett-packard / Sd2100
Subcategory: Batch
Comment:

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

Thanks
Brock



Sponsored Link
Ads by Google

Response Number 1
Name: brockey01
Date: April 11, 2009 at 13:32:59 Pacific
Reply:

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.


0

Response Number 2
Name: Mechanix2Go
Date: April 11, 2009 at 16:42:34 Pacific
Reply:

@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


0

Response Number 3
Name: brockey01
Date: April 11, 2009 at 18:12:11 Pacific
Reply:

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.


0

Response Number 4
Name: Judago
Date: April 11, 2009 at 19:28:03 Pacific
Reply:

%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


0

Response Number 5
Name: Mechanix2Go
Date: April 11, 2009 at 23:39:43 Pacific
Reply:

Judago,

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


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

M2


0

Related Posts

See More



Response Number 6
Name: Judago
Date: April 12, 2009 at 00:46:22 Pacific
Reply:

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.


0

Response Number 7
Name: Mechanix2Go
Date: April 12, 2009 at 00:52:25 Pacific
Reply:

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


0

Response Number 8
Name: Judago
Date: April 12, 2009 at 01:15:37 Pacific
Reply:

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


0

Response Number 9
Name: reno
Date: April 12, 2009 at 01:48:22 Pacific
Reply:

any reason not using random mod 100 + 1 ?


0

Response Number 10
Name: Judago
Date: April 12, 2009 at 06:23:43 Pacific
Reply:

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

Subcategory: Batch


0

Response Number 11
Name: reno
Date: April 12, 2009 at 07:24:10 Pacific
Reply:

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


0

Response Number 12
Name: BatchFreak
Date: April 12, 2009 at 08:48:38 Pacific
Reply:

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.


0

Response Number 13
Name: lee123abc
Date: April 12, 2009 at 10:26:41 Pacific
Reply:

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

Brockey01... you happy with the answers?


0

Response Number 14
Name: brockey01
Date: April 12, 2009 at 15:34:04 Pacific
Reply:

how come this doesnt work?

this should generate 1 to 100 only but doesnt.


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


0

Response Number 15
Name: BatchFreak
Date: April 12, 2009 at 19:32:49 Pacific
Reply:

thats a little bit of overkill lol

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


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch random number generator

Random number generator www.computing.net/answers/programming/random-number-generator/12990.html

Random Number Generator in C www.computing.net/answers/programming/random-number-generator-in-c/9566.html

HTML Random Number Generator Questi www.computing.net/answers/programming/html-random-number-generator-questi/13824.html