Page 1 of 1

set %RANDOM% for timeout command

Posted: 08 Apr 2019 10:22
by Mado91
Goodevening programmers,
here I have a trouble with a .bat file I'm working on:
I want do make an action (for example: start notepad.exe) over and over with a "random" timeout with the following code:

Code: Select all

:label
start notepad.exe
timeout %RANDOM%
goto label
Now I need to "set" the random number, for example, > 1 and < 100, so I tryed using the following code:

Code: Select all

SET /A test=%RANDOM%*100/32768+1
:label
start notepad.exe
timeout test
goto label
This returns me error. What can I do to make it work? Thanks everybody

Re: set %RANDOM% for timeout command

Posted: 08 Apr 2019 12:19
by aGerman
timeout test
You forgot the percent signs.

Steffen

Re: set %RANDOM% for timeout command

Posted: 08 Apr 2019 22:42
by Mado91
Of course! Thanks a lot! :lol:

...........................................________
....................................,.-'"...................``~.,
.............................,.-"..................................."-.,
.........................,/...............................................":,
.....................,?......................................................,
.................../...........................................................,}
................./......................................................,:`^`..}
.............../...................................................,:"........./
..............?.....__.........................................:`.........../
............./__.(....."~-,_..............................,:`........../
.........../(_...."~,_........"~,_....................,:`........_/
..........{.._$;_......"=,_......."-,_.......,.-~-,},.~";/....}
...........((.....*~_......."=-._......";,,./`..../"............../
...,,,___.`~,......"~.,....................`.....}............../
............(....`=-,,.......`........................(......;_,,-"
............/.`~,......`-...................................../
.............`~.*-,.....................................|,./.....,__
,,_..........}.>-._...................................|..............`=~-,
.....`=~-,__......`,.................................
...................`=~-,,.,...............................
................................`:,,...........................`..............__
.....................................`=-,...................,%`>--==``
........................................_..........._,-%.......`
...................................,

Re: set %RANDOM% for timeout command

Posted: 09 Apr 2019 08:41
by Squashman
A more concise way to get a number between 1 and 100.

Code: Select all

SET /A test=%RANDOM% %% 100 + 1

Re: set %RANDOM% for timeout command

Posted: 09 Apr 2019 10:30
by aGerman
Good enough for that purpose. The reason why I didn't propose the modulus calculation is that the results have a slight displacement towards lower values. Division by 32768 generates a better distribution.

Steffen

Re: set %RANDOM% for timeout command

Posted: 09 Apr 2019 23:01
by Mado91
Thanks evefrybody for the tips :) .
Still I encountered anoter problem!
I wrote the following working code:

Code: Select all

:labely
start notepad.exe
SET /A watch=%RANDOM% %% 2400
timeout %watch%
goto labely
Then I need to insert it in a bigger program, here the full example code:

Code: Select all

@ECHO OFF 
title BulBot 
color 70 
:masterlabel 
set /P input= 
IF %input% EQU clear ( 
CLS 
) 
IF %input% EQU c ( 
start C:\ 
) 
IF %input% EQU readme ( 
start C:\BulBot\readme.txt 
) 
IF %input% EQU n ( 
:labely
start notepad.exe
SET /A watch=%RANDOM% %% 2400
timeout %watch%
goto labely
) 
goto masterlabel 
This return me and error at the timeout command row,
does the two loop fight themselves? how?
Thanks everybody

Re: set %RANDOM% for timeout command

Posted: 10 Apr 2019 05:52
by Compo
You're setting the variable %watch%, whilst in a parenthesised block of code. In order to use the new value, you need to delay the expansion of the variable at run time, as opposed to read time.

Code: Select all

@Echo Off
Title BulBot
Color 70

:MasterLabel
Set /P "input="
If /I "%input%"=="clear" ClS
If /I "%input%"=="c" Start "" "C:"
If /I "%input%"=="readme" Start "" "C:\BulBot\readme.txt"
If /I "%input%"=="n" (
	:Labely
	Start notepad
	SetLocal EnableDelayedExpansion
	Set /A watch=!RANDOM! %% 2400
	Timeout !watch!
	EndLocal
	GoTo Labely
)
GoTo MasterLabel
I would also suggest, if you don't have a hufe list of options to verify with If, that you consider the Choice command instead of using Set /P. Open a Command Prompt window and enter Choice /? to read its usage information.

Re: set %RANDOM% for timeout command

Posted: 10 Apr 2019 10:39
by Mado91
This suits perfectly! Thanks a lot :)