Sleep: Difference between revisions

From LemonWiki共筆
Jump to navigation Jump to search
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
Sleep random seconds in programming
Sleep random seconds in programming


== MS-DOS ==
== MS-DOS on Windows ==
=== Approach 1: %RANDOM% + ping local ===
=== Approach 1: %RANDOM% + ping local ===
[https://stackoverflow.com/questions/8258087/bat-random-timeout batch file - .bat random timeout - Stack Overflow]
[https://stackoverflow.com/questions/8258087/bat-random-timeout batch file - .bat random timeout - Stack Overflow]
Line 24: Line 24:


=== 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 one of following command
# 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>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>}}.
#* 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>}}.
Line 68: Line 68:


=== Approach1: sleep ===
=== 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>
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>
<pre>
# print current date & time
# print current date & time
Line 79: Line 79:
# 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>
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>
</pre>


Line 84: Line 111:
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>
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 99: Line 126:


=== Approach3: usleep ===
=== 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)
[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>
<pre>
# print current date & time
# print current date, time & nanoseconds
echo $(date '+%Y-%m-%d %H:%M:%S')
echo $(date '+%Y-%m-%d %H:%M:%S')$(date '+.%N')


# sleep 0.2 ~ 1.2 seconds
# sleep 0.2 ~ 1.2 seconds
Line 110: Line 137:
usleep $timeout
usleep $timeout


# print current date & time
# print current date, time & nanoseconds
echo $(date '+%Y-%m-%d %H:%M:%S')
echo $(date '+%Y-%m-%d %H:%M:%S')$(date '+.%N')


</pre>
</pre>
Line 118: Line 145:
* [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]


== References ==
== References ==
Line 128: Line 158:
* [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]


[[Category:Programming]] [[Category:PHP]] [[Category:MS-DOS]] [[Category:Tool]]
[[Category:Programming]] [[Category:PHP]] [[Category:MS-DOS]] [[Category:Bash]] [[Category:Tool]]

Revision as of 11:10, 14 October 2021

Sleep random seconds in programming

MS-DOS on Windows

Approach 1: %RANDOM% + ping local

batch file - .bat random timeout - Stack Overflow

  • %RANDOM% returns an integer between 0 and 32767.
  • 60 is the range of values you want: 0 to 60.
  • 32768 is the range of values returned by %RANDOM% (0 to 32767).
  • 5 is the minimum value you want. The original range of values you want '0 to 60' became '5 ~ 65'.
  • ping local address: /n <Count> "Specifies the number of echo Request messages sent. The default is 4."[1][2]

BAT file:

REM print current date & time
ECHO %date% %time%

REM sleep 5 ~ 65 seconds
SET /a timeout=%RANDOM% * 60 / 32768 + 5
ping 127.0.0.1 -n %timeout% > nul

REM print current date & time
ECHO %date% %time%

Approach 2: sleep package of CygWin

Requirement: Install Cygwin on Win Os windows.png & install sleep package (How-to: Setting Up Cygwin/X)

Verify the installation of sleep package[3][4]

  1. Key-in cmd to open command prompt (How to Open Command Prompt (Windows 10, 8, 7, Vista, XP))
  2. Key-in one of following commands
    • Key-in where sleep if you add installation path of CygWin to system path (How to setup my system path). Or
    • Key-in dir C:\cygwin64\bin\sleep.exe if the installation path of CygWin is C:\cygwin64\.
C:\Users\user>dir C:\cygwin64\bin\sleep.exe

# successful condition
C:\cygwin64\bin\sleep.exe

# failed condition
File Not Found

BAT file:

REM print current date & time
ECHO %date% %time%

REM sleep 5 ~ 65 seconds
SET /a timeout=%RANDOM% * 60 / 32768 + 5
C:\cygwin64\bin\sleep.exe %timeout%

REM print current date & time
ECHO %date% %time%

Approach 3: TIMEOUT

Alternative command: Icon_exclaim.gif The following command ECHO %date% %time% will not be executed after TIMEOUT %timeout% was executed.

BAT file:

SET /a timeout=%RANDOM% * 60 / 32768 + 5
TIMEOUT %timeout%

ECHO %date% %time%

BASH

Approach1: sleep

Tested on Linux Os linux.png & Mac icon_os_mac.png [5][6]

# 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')

Sleep couple microseconds[7]. Tested on Linux Os linux.png

# 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


Sleep couple microseconds. Tested on Mac icon_os_mac.png

# 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;'

Approach2: ping

Tested on Linux Os linux.png & Mac icon_os_mac.png

ping local address: -c <Count> "Stop after sending (and receiving) count ECHO_RESPONSE packets."[8]

# print current date & time
echo $(date '+%Y-%m-%d %H:%M:%S')

# sleep 5 ~ 65 seconds
timeout=$(($RANDOM * 60 / 32768 + 5))
ping 127.0.0.1 -c $timeout > nul

# print current date & time
echo $(date '+%Y-%m-%d %H:%M:%S')

Approach3: usleep

usleep(3) - Linux man page "suspend execution for microsecond intervals" (1 second = 1 000 000 microsecond). Tested on Linux Os linux.png

# 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')

PHP

JavaScript

References

Related