[SOLVED] SET /a -- Random Number?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
taripo
Posts: 228
Joined: 01 Aug 2011 13:48

Re: [SOLVED] SET /a -- Random Number?

#16 Post by taripo » 27 Nov 2011 16:26

del
Last edited by taripo on 13 Dec 2011 15:30, edited 3 times in total.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: [SOLVED] SET /a -- Random Number?

#17 Post by Ed Dyreen » 27 Nov 2011 16:43

'
hmm, do you really need command.COM :?:

Try .CMD for batch extension...

taripo
Posts: 228
Joined: 01 Aug 2011 13:48

Re: [SOLVED] SET /a -- Random Number?

#18 Post by taripo » 27 Nov 2011 17:04

del
Last edited by taripo on 13 Dec 2011 15:31, edited 1 time in total.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: [SOLVED] SET /a -- Random Number?

#19 Post by Ed Dyreen » 27 Nov 2011 17:23

'
I get a decimal value 166 and 167 from 0xA6, and 0xA7, however ¦ is dec 221 = 0xDD

taripo
Posts: 228
Joined: 01 Aug 2011 13:48

Re: [SOLVED] SET /a -- Random Number?

#20 Post by taripo » 27 Nov 2011 17:44

del
Last edited by taripo on 13 Dec 2011 15:31, edited 2 times in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: [SOLVED] SET /a -- Random Number?

#21 Post by aGerman » 27 Nov 2011 18:24

It depends on your locale settings which codepages are used. You will find them in the registry.

Code: Select all

@echo off &setlocal
for /f "tokens=2*" %%i in ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\Nls\CodePage" /v "OEMCP"') do set /a OEMCP=%%j
for /f "tokens=2*" %%i in ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\Nls\CodePage" /v "ACP"') do set /a ACP=%%j
set OEMCP
set ACP
pause>nul

That code returns for my German settings:

Code: Select all

OEMCP=850
ACP=1252

Means codepage 850 (ASCII) and codepage 1252 (ANSI).

If you save your code in ANSI it is however interpreted in ASCII in your command window. For that reason some characters are not displayed in the same manner.
E.g. Hex 0xA9 represents character © in codepage 1252 but character ® in codepage 850.
(ref. http://en.wikipedia.org/wiki/Code_page_850, http://en.wikipedia.org/wiki/Windows-1252)

BTW: That discussion is a bit off topic in a "SET /A" thread, isn't it :wink:

Regards
aGerman

taripo
Posts: 228
Joined: 01 Aug 2011 13:48

Re: [SOLVED] SET /a -- Random Number?

#22 Post by taripo » 27 Nov 2011 18:52

Note: agerman, you're welcome to delete your post since it's in the first of the 2 links here
a few issues about encodings
viewtopic.php?f=3&t=2647&p=12098#p12098
viewtopic.php?f=3&t=2550
Last edited by taripo on 13 Dec 2011 15:28, edited 2 times in total.

taripo
Posts: 228
Joined: 01 Aug 2011 13:48

Re: [SOLVED] SET /a -- Random Number?

#23 Post by taripo » 13 Dec 2011 15:04

Ed, i'm just looking into your great macro idea.. and I see dave benham's great posts here and jeb's recent thing.. which i'll have to study some time.. and will include for reference here "Macros with parameters appended" viewtopic.php?f=3&t=2518

on a similar note to something in dave benham's post

since you can do the macro with one FOR statement, not two.. By skipping the first FOR

Do you actually prefer doing it with the FOR statement to set
Get.RandomNumber.TokenSTR

When you could use just use Set alone.

so that first For line could be replaced with that set line..

I suppose if you've used 2 FOR statements in other macros then this would apply to any of your macros.. Where you could use one FOR instead of 2. I see dave benham for example did it with one FOR.

Everything to the right of the first = in each for loop is identical.

Code: Select all

:: for %%? in ( Get.RandomNumber.TokenSTR ) do set "%%~?=set /a Minimum = %%~b ^&set /a Maximum = %%~c ^&set /a interval = ^^^!Maximum^^^! - ^^^!Minimum^^^! ^&call set /a %%~d = %%Random%% ^&set /a %%~d = ^^^!%%~d^^^! * ^^^!interval^^^! / 32767 + ^^^!Minimum^^^! ^&( echo. ^&echo. ^&set /p "?= %%~d : '^^^!%%~d^^^!' [OK]" ^<nul ) "


set "Get.RandomNumber.TokenSTR=set /a Minimum = %%~b ^&set /a Maximum = %%~c ^&set /a interval = ^^^!Maximum^^^! - ^^^!Minimum^^^! ^&call set /a %%~d = %%Random%% ^&set /a %%~d = ^^^!%%~d^^^! * ^^^!interval^^^! / 32767 + ^^^!Minimum^^^! ^&( echo. ^&echo. ^&set /p "?= %%~d : '^^^!%%~d^^^!' [OK]" ^<nul ) "

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: [SOLVED] SET /a -- Random Number?

#24 Post by Ed Dyreen » 13 Dec 2011 17:42

'
taripo wrote:Do you actually prefer doing it with the FOR statement to set
Get.RandomNumber.TokenSTR
O that ?, ignore it, totally outdate...

This macro has evolved significantly. Unfortunately it is now interconnected and is useless without significantly rewriting it to use it outside it's library. This is a problem on a DOSTips forum :?

Code: Select all

%   % set ^"%$defines%=!Jdelim_! ( %$n1c%
%      % call set /a %%~a = %%Random%% %%%% %E7%( %%~c - %%~b + 1 %E7%) + %%~b ^|^|!@gErr! %$n1c%
%   % ) else set $="

Code: Select all

>con ( %Random_% $var, 0, 999 ) &&echo.$var=!$var!_ ||echo.I see problem: '!$err!' ^!

taripo
Posts: 228
Joined: 01 Aug 2011 13:48

Re: [SOLVED] SET /a -- Random Number?

#25 Post by taripo » 14 Dec 2011 05:58

Ed Dyreen wrote:'
taripo wrote:Do you actually prefer doing it with the FOR statement to set
Get.RandomNumber.TokenSTR
O that ?, ignore it, totally outdate...

This macro has evolved significantly. Unfortunately it is now interconnected and is useless without significantly rewriting it to use it outside it's library. This is a problem on a DOSTips forum :?

Code: Select all

%   % set ^"%$defines%=!Jdelim_! ( %$n1c%
%      % call set /a %%~a = %%Random%% %%%% %E7%( %%~c - %%~b + 1 %E7%) + %%~b ^|^|!@gErr! %$n1c%
%   % ) else set $="

Code: Select all

>con ( %Random_% $var, 0, 999 ) &&echo.$var=!$var!_ ||echo.I see problem: '!$err!' ^!


looks interesting.. I like the fact that the old version is still there 'cos I still learn a bit about how the batch language works or can work, from playing with code like yours!

Post Reply