14,974
edits
No edit summary |
m (→Related) Tags: Mobile edit Mobile web edit |
||
| (33 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
Sleep random seconds in programming | Sleep random seconds in programming | ||
== MS-DOS == | {{LanguageSwitcher | content = [[Sleep | EN]], [[Sleep in Mandarin | 漢字]] }} | ||
== MS-DOS on Windows == | |||
=== Approach 1: %RANDOM% + ping local === | === Approach 1: %RANDOM% + ping local === | ||
Purpose | |||
* | * Sleep random seconds between 5 ~ 60 seconds | ||
BAT file: | BAT file: | ||
< | <syntaxhighlight lang="Windows batch files"> | ||
REM print current date & time | REM print current date & time | ||
ECHO %date% %time% | ECHO %date% %time% | ||
REM sleep 5 ~ | REM sleep 5 ~ 60 seconds | ||
SET /a timeout=%RANDOM% * | SET /a timeout=%RANDOM% * 55 / 32767 + 5 | ||
ping 127.0.0.1 -n %timeout% > nul | ping 127.0.0.1 -n %timeout% > nul | ||
REM print current date & time | REM print current date & time | ||
ECHO %date% %time% | ECHO %date% %time% | ||
</ | </syntaxhighlight> | ||
Instruction<ref>[https://stackoverflow.com/questions/8258087/bat-random-timeout batch file - .bat random timeout - Stack Overflow]</ref> | |||
* {{kbd | key=<nowiki>%RANDOM%</nowiki>}} returns an integer between 0 and 32767<ref>[https://ss64.com/nt/syntax-random.html Random Numbers - Windows CMD - SS64.com]</ref>. | |||
* 55 is the range of values you want: 5 to 60. | |||
* 32767 is the range of values returned by {{kbd | key=<nowiki>%RANDOM%</nowiki>}} (0 to 32767). | |||
* 5 is the minimum value you want. The original range of values is '0 to 55'. After plus 5 the range became '5 ~ 65'. | |||
* ping local address: {{kbd | key=<nowiki>/n <Count></nowiki>}} "Specifies the number of echo Request messages sent. The default is 4."<ref>[https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/ping time]</ref><ref>[https://stackoverflow.com/questions/735285/how-to-wait-in-a-batch-script windows xp - How to wait in a batch script? - Stack Overflow]</ref> | |||
=== Approach 2: sleep package of CygWin === | === Approach 2: sleep package of CygWin === | ||
Requirement: Install [https://cygwin.com/install.html Cygwin] on {{Win}} & install {{kbd | key=<nowiki>sleep</nowiki>}} package | Requirement: Install [https://cygwin.com/install.html Cygwin] on {{Win}} & install {{kbd | key=<nowiki>sleep</nowiki>}} package (How-to: [https://x.cygwin.com/docs/ug/setup.html Setting Up Cygwin/X]) | ||
Verify the installation of {{kbd | key=<nowiki>sleep</nowiki>}} package<ref>[https://stackoverflow.com/questions/9260014/how-do-i-install-cygwin-components-from-the-command-line How do I install cygwin components from the command line? - Stack Overflow]</ref><ref>[http://polarhome.com/service/man/?qf=SLEEP&af=0&tf=2&of=Cygwin SLEEP man page on Cygwin]</ref> | Verify the installation of {{kbd | key=<nowiki>sleep</nowiki>}} package<ref>[https://stackoverflow.com/questions/9260014/how-do-i-install-cygwin-components-from-the-command-line How do I install cygwin components from the command line? - Stack Overflow]</ref><ref>[http://polarhome.com/service/man/?qf=SLEEP&af=0&tf=2&of=Cygwin SLEEP man page on Cygwin]</ref> | ||
# Key-in {{kbd | key=<nowiki>cmd</nowiki>}} to open command prompt ([https://www.lifewire.com/how-to-open-command-prompt-2618089 How to Open Command Prompt (Windows 10, 8, 7, Vista, XP)]) | # Key-in {{kbd | key=<nowiki>cmd</nowiki>}} to open command prompt ([https://www.lifewire.com/how-to-open-command-prompt-2618089 How to Open Command Prompt (Windows 10, 8, 7, Vista, XP)]) | ||
# Key-in {{kbd | key=<nowiki>where sleep</nowiki>}} | # Key-in one of following commands | ||
#* Key-in {{kbd | key=<nowiki>where sleep</nowiki>}} if you add installation path of CygWin to system path ([[How to setup my system path]]). Or | |||
#* Key-in {{kbd | key=<nowiki>dir C:\cygwin64\bin\sleep.exe</nowiki>}} if the installation path of CygWin is {{kbd | key=<nowiki>C:\cygwin64\</nowiki>}}. | |||
<pre> | <pre> | ||
C:\Users\user>dir C:\cygwin64\bin\sleep.exe | |||
# successful condition | # successful condition | ||
C:\cygwin64\bin\sleep.exe | C:\cygwin64\bin\sleep.exe | ||
| Line 43: | Line 51: | ||
ECHO %date% %time% | ECHO %date% %time% | ||
REM sleep 5 ~ | REM sleep 5 ~ 60 seconds | ||
SET /a timeout=%RANDOM% * | SET /a timeout=%RANDOM% * 55 / 32767 + 5 | ||
C:\cygwin64\bin\sleep.exe %timeout% | C:\cygwin64\bin\sleep.exe %timeout% | ||
| Line 56: | Line 64: | ||
BAT file: | BAT file: | ||
<pre> | <pre> | ||
SET /a timeout=%RANDOM% * | SET /a timeout=%RANDOM% * 55 / 32767 + 5 | ||
TIMEOUT %timeout% | TIMEOUT %timeout% | ||
| Line 63: | Line 71: | ||
== BASH == | == BASH == | ||
=== Approach1: sleep === | |||
Tested on {{Linux}} & {{Mac}}<ref>[https://bash.cyberciti.biz/guide/Perform_arithmetic_operations Perform arithmetic operations - Linux Shell Scripting Tutorial - A Beginner's handbook]</ref><ref>[https://linuxize.com/post/how-to-use-linux-sleep-command-to-pause-a-bash-script/ Linux Sleep Command (Pause a Bash Script) | Linuxize]</ref> | |||
<pre> | |||
# print current date & time | |||
echo $(date '+%Y-%m-%d %H:%M:%S') | |||
# sleep 5 ~ 65 seconds | |||
timeout=$(($RANDOM * 60 / 32768 + 5)) | |||
sleep $timeout | |||
# print current date & time | |||
echo $(date '+%Y-%m-%d %H:%M:%S') | |||
</pre> | |||
Sleep couple [https://en.wikipedia.org/wiki/Microsecond microsecond]s<ref>[https://serverfault.com/questions/151109/how-do-i-get-the-current-unix-time-in-milliseconds-in-bash How do I get the current Unix time in milliseconds in Bash? - Server Fault]</ref>. Tested on {{Linux}} | |||
<pre> | |||
# print current unix timestamp e.g. 1598346362244 | |||
date +%s%N | |||
# sleep 1 millisecond = 0.001 seconds | |||
sleep 0.001s | |||
# print current unix timestamp | |||
date +%s%N | |||
</pre> | |||
Sleep couple microseconds. Tested on {{Mac}} | |||
<pre> | |||
# print current unix timestamp e.g. 1598346362244 | |||
php -r 'echo microtime(TRUE) . PHP_EOL;' | |||
# sleep 1 millisecond = 0.001 seconds | |||
sleep 0.001s | |||
# print current unix timestamp | |||
php -r 'echo microtime(TRUE) . PHP_EOL;' | |||
</pre> | |||
Sleep couple microseconds. Tested on {{Mac}} | |||
<pre> | |||
# print current unix timestamp e.g. 1598346362244 | |||
Sleep couple microseconds. Tested on {{Mac}} | |||
<pre> | |||
# print current unix timestamp e.g. 1598346362244 | |||
php -r 'echo microtime(TRUE) . PHP_EOL;' | |||
# sleep 1 millisecond = 0.001 seconds | |||
sleep 0.001s | |||
# print current unix timestamp | |||
php -r 'echo microtime(TRUE) . PHP_EOL;' | |||
</pre> | |||
Sleep couple milliseconds (1 seconds = 1000 milliseconds) e.g. 1671090194.136471987<ref>[https://apple.stackexchange.com/questions/135742/time-in-milliseconds-since-epoch-in-the-terminal macos - Time in milliseconds since epoch in the terminal - Ask Different]</ref> | |||
<pre> | |||
# print current unix timestamp in milliseconds e.g. 1671090194.136471987 | |||
perl -MTime::HiRes=time -e 'printf "%.9f\n", time' | |||
# sleep 1 millisecond = 0.001 seconds | |||
sleep 0.001s | |||
# print current unix timestamp | |||
perl -MTime::HiRes=time -e 'printf "%.9f\n", time' | |||
</pre> | |||
=== Approach2: ping === | |||
Tested on {{Linux}} & {{Mac}} | Tested on {{Linux}} & {{Mac}} | ||
ping local address: {{kbd | key=<nowiki>-c <Count></nowiki>}} "Stop after sending (and receiving) count ECHO_RESPONSE packets."<ref>[https://linux.die.net/man/8/ping ping(8) - Linux man page]</ref> | |||
<pre> | <pre> | ||
| Line 71: | Line 155: | ||
# sleep 5 ~ 65 seconds | # sleep 5 ~ 65 seconds | ||
timeout=$(($RANDOM * 60 / 32768 + 5)) | timeout=$(($RANDOM * 60 / 32768 + 5)) | ||
ping 127.0.0.1 -c $timeout > nul | |||
# print current date & time | # print current date & time | ||
echo $(date '+%Y-%m-%d %H:%M:%S') | echo $(date '+%Y-%m-%d %H:%M:%S') | ||
</pre> | </pre> | ||
=== Approach3: usleep === | |||
[https://linux.die.net/man/3/usleep usleep(3) - Linux man page] "suspend execution for microsecond intervals" (1 second = 1 000 000 microsecond). Tested on {{Linux}} | |||
<pre> | |||
# print current date, time & nanoseconds | |||
echo $(date '+%Y-%m-%d %H:%M:%S')$(date '+.%N') | |||
# sleep 0.2 ~ 1.2 seconds | |||
timeout=$(($RANDOM * 1000000 / 32768 + 200000)) | |||
echo $timeout | |||
usleep $timeout | |||
# print current date, time & nanoseconds | |||
echo $(date '+%Y-%m-%d %H:%M:%S')$(date '+.%N') | |||
</pre> | |||
=== BASH example: download file and sleep random seconds between 1 ~ 7 seconds === | |||
The following script checks if a specific file exists in a defined path. If the file doesn't exist, it downloads the file from a provided URL and then pauses for a random duration between 1 and 7 seconds. If the file already exists, it simply prints a message stating that the file was found and the download was skipped. | |||
<pre> | |||
if [ ! -f /path/to/paper.pdf ] | |||
then | |||
curl "https://website/to/pdf" -L -o /path/to/paper.pdf | |||
sleep $((1 + RANDOM % 7)) | |||
else | |||
echo "File found. Download skipped." | |||
fi | |||
</pre> | |||
The same command can be written on one line: | |||
<pre> | |||
if [ ! -f /path/to/paper.pdf ]; then curl "https://website/to/pdf" -L -o /path/to/paper.pdf; sleep $((1 + RANDOM % 7)); else echo "File found. Download skipped."; fi | |||
</pre> | |||
References | |||
* [https://www.cyberciti.biz/faq/bash-check-if-file-does-not-exist-linux-unix/ How to check if file does not exist in Bash - nixCraft] | |||
== PHP == | == PHP == | ||
* [http://php.net/manual/en/function.sleep.php PHP: sleep - Manual] | * [http://php.net/manual/en/function.sleep.php PHP: sleep - Manual] | ||
* [http://php.net/manual/en/function.usleep.php PHP: usleep - Manual] | * [http://php.net/manual/en/function.usleep.php PHP: usleep - Manual] | ||
== JavaScript == | |||
* [https://www.w3schools.com/js/js_timing.asp JavaScript Timing Events] | |||
* [https://www.educative.io/edpresso/what-is-the-javascript-alternative-to-the-sleep-function What is the Javascript alternative to the sleep function?] | |||
== References == | == References == | ||
| Line 89: | Line 217: | ||
* [https://en.wikipedia.org/wiki/Sleep_(system_call) Sleep (system call) - Wikipedia] | * [https://en.wikipedia.org/wiki/Sleep_(system_call) Sleep (system call) - Wikipedia] | ||
* [https://blog.gtwang.org/windows/excel-random-number-generation-formula/ Excel 產生各種亂數的公式整理與函數教學 - G. T. Wang] | * [https://blog.gtwang.org/windows/excel-random-number-generation-formula/ Excel 產生各種亂數的公式整理與函數教學 - G. T. Wang] | ||
* [https://blog.miniasp.com/post/2009/06/24/Sleep-command-in-Batch 如何在批次檔(Batch)中實現 sleep 命令讓任務暫停執行 n 秒 | The Will Will Web] | |||
* [https://errerrors.blogspot.com/2024/02/implement-randbetween-using-round.html Excel 使用 RAND 函數實作 RANDBETWEEN 函數] | |||
[[Category:Programming]] [[Category:PHP]] [[Category:MS-DOS]] [[Category:Tool]] | [[Category:Programming]] [[Category:PHP]] [[Category:MS-DOS]] [[Category:Bash]] [[Category:Tool]] | ||