set %RANDOM% for timeout command

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Mado91
Posts: 13
Joined: 16 Mar 2019 08:03

set %RANDOM% for timeout command

#1 Post by Mado91 » 08 Apr 2019 10:22

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

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

Re: set %RANDOM% for timeout command

#2 Post by aGerman » 08 Apr 2019 12:19

timeout test
You forgot the percent signs.

Steffen

Mado91
Posts: 13
Joined: 16 Mar 2019 08:03

Re: set %RANDOM% for timeout command

#3 Post by Mado91 » 08 Apr 2019 22:42

Of course! Thanks a lot! :lol:

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

Squashman
Expert
Posts: 4097
Joined: 23 Dec 2011 13:59

Re: set %RANDOM% for timeout command

#4 Post by Squashman » 09 Apr 2019 08:41

A more concise way to get a number between 1 and 100.

Code: Select all

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

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

Re: set %RANDOM% for timeout command

#5 Post by aGerman » 09 Apr 2019 10:30

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

Mado91
Posts: 13
Joined: 16 Mar 2019 08:03

Re: set %RANDOM% for timeout command

#6 Post by Mado91 » 09 Apr 2019 23:01

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

Compo
Posts: 487
Joined: 21 Mar 2014 08:50

Re: set %RANDOM% for timeout command

#7 Post by Compo » 10 Apr 2019 05:52

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.

Mado91
Posts: 13
Joined: 16 Mar 2019 08:03

Re: set %RANDOM% for timeout command

#8 Post by Mado91 » 10 Apr 2019 10:39

This suits perfectly! Thanks a lot :)

Post Reply