Skip to content

Commit

Permalink
Add Random
Browse files Browse the repository at this point in the history
  • Loading branch information
phil294 committed Jul 20, 2023
1 parent c14bad3 commit c9f78bd
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 22 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ AHK_X11 can be used completely without a terminal. You can however if you want u
<details><summary><strong>CLICK TO SEE WHICH COMMANDS ARE IMPLEMENTED AND WHICH ARE MISSING</strong>. Note however that this is not very representative. For example, no `Gui` sub command is included in the listing. For a better overview on what is already done, skim through the <a href="https://phil294.github.io/AHK_X11"><b>FULL DOCUMENTATION HERE</b></a>.</summary>

```diff
DONE ?% (101/220):
DONE ?% (102/220):
+ Else, { ... }, Break, Continue, Return, Exit, GoSub, GoTo, IfEqual, Loop, SetEnv, Sleep, FileCopy,
+ SetTimer, WinActivate, MsgBox, Gui, SendRaw, #Persistent, ExitApp,
+ EnvAdd, EnvSub, EnvMult, EnvDiv, ControlSendRaw, IfWinExist/IfWinNotExist, SetWorkingDir,
Expand All @@ -70,7 +70,7 @@ DONE ?% (101/220):
+ WinGetTitle, WinGetClass, PixelGetColor, CoordMode, GuiControl, ControlGetPos, ControlGetText,
+ WinGet, Input, Loop (parse a string), ToolTip, If var [not] in/contains MatchList, ControlSetText,
+ PixelSearch, #Include, InputBox, ClipWait, EnvSet, SetKeyDelay, SetMouseDelay, MouseClickDrag,
+ #NoTrayIcon, TrayTip
+ #NoTrayIcon, TrayTip, Random

NEW ?% (9/220): (not part of spec or from a more recent version)
@@ Echo, ahk_x11_print_vars, FileRead, RegExGetPos, RegExReplace, EnvGet, Click @@
Expand All @@ -85,7 +85,7 @@ REMOVED ?% (11/220):
# AutoTrim: It's always Off. It would not differentiate between %a_space% and %some_var%.
# It's possible but needs significant work.

TO DO ?% (95/220): alphabetically
TO DO ?% (94/220): alphabetically
- BlockInput, Control, ControlFocus, ControlGet, ControlGetFocus,
- ControlMove,
- DetectHiddenText, DetectHiddenWindows, Drive, DriveGet, DriveSpaceFree,
Expand All @@ -97,7 +97,7 @@ TO DO ?% (95/220): alphabetically
- If var is [not] type,
- KeyHistory, ListHotkeys, ListLines, ListVars,
- OnExit,
- Process, Progress, Random, RunAs, SetBatchLines,
- Process, Progress, RunAs, SetBatchLines,
- SetCapslockState, SetControlDelay, SetDefaultMouseSpeed, SetFormat,
- SetNumlockState, SetScrollLockState, SetStoreCapslockMode, SetTitleMatchMode,
- SetWinDelay, Shutdown, Sort, SoundGet, SoundGetWaveVolume, SoundPlay, SoundSet,
Expand Down
39 changes: 21 additions & 18 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ <h2>Table of contents </h2>
<a href="#IfIs.htm">If var is [not] type</a>
</li>
<li>
<a class="tbd" href="#Random.htm">Random</a>
<a href="#Random.htm">Random</a>
</li>
<li>
<a class="tbd" href="#SetFormat.htm">SetFormat</a>
Expand Down Expand Up @@ -2478,7 +2478,7 @@ <h2 class="calibre9"><span class="calibre23">The "Last Found" Window </span></h2
<td height="16" class="calibre4">Creates or modifies a window to display a progress bar.</td>
</tr>
<tr class="calibre3">
<td height="16" class="tbd calibre4"><a href="#Random.htm" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">Random</a></td>
<td height="16" class="calibre4"><a href="#Random.htm" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">Random</a></td>
<td height="16" class="calibre4">Generates a pseudo-random number.</td>
</tr>
<tr class="calibre3">
Expand Down Expand Up @@ -9199,7 +9199,7 @@ <h2 class="calibre9"><span class="calibre23">The "Last Found" Window </span></h2
MsgBox, %var% is also a valid date-time.</pre>
</div>
</div>
<div class="calibreMain tbd">
<div class="calibreMain">
<div class="calibreEbookContent">
<a id="Random.htm" href="#Random.htm">#</a> <h2 class="calibre17">Random</h2>
<hr size="2" class="calibre24" />
Expand Down Expand Up @@ -9242,20 +9242,23 @@ <h2 class="calibre9"><span class="calibre23">The "Last Found" Window </span></h2
<p class="calibre8"> Random, rand, 1, 10<br class="calibre12" /> Random, rand, 0.0, 1.0<br class="calibre12" />
<br class="calibre12" />
</p>
<p class="calibre8">Comments based on the original source</p>
<p class="calibre8">This function uses the Mersenne Twister random number generator, MT19937, written by Takuji Nishimura and Makoto Matsumoto, Shawn Cokus, Matthe Bellew and Isaku Wada.</p>
<p class="calibre8">The Mersenne Twister is an algorithm for generating random numbers. It was designed with consideration of the flaws in various other generators. The period, 2<sup class="calibre36">19937</sup>-1, and the order of equidistribution, 623 dimensions, are far greater. The generator is also fast; it avoids multiplication and division, and it benefits from caches and pipelines. For more information see the inventors' web page at http://www.math.keio.ac.jp/~matumoto/emt.html</p>
<p class="calibre8">Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, All rights reserved.</p>
<p class="calibre8">Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:</p>
<ol class="calibre19">
<li class="calibre11">Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.</li>
<li class="calibre11">Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.</li>
<li class="calibre11">The names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission.</li>
</ol>
<p class="calibre8">THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</p>
<p class="calibre8">Do NOT use for CRYPTOGRAPHY without securely hashing several returned values together, otherwise the generator state can be learned after reading 624 consecutive values.</p>
<p class="calibre8">When you use this, send an email to: matumoto@math.keio.ac.jp with an appropriate reference to your work. It would be nice to CC: rjwagner@writeme.com and Cokus@math.washington.edu when you write.</p>
<p class="calibre8"><i class="calibre21">This above has been already been done for AutoHotkey, but if you use the Random command in a publicly distributed application, consider sending an e-mail to the above people to thank them for their efforts and generosity.</i></p>
<div class="rm">
<p class="calibre8">Comments based on the original source</p>
<p class="calibre8">This function uses the Mersenne Twister random number generator, MT19937, written by Takuji Nishimura and Makoto Matsumoto, Shawn Cokus, Matthe Bellew and Isaku Wada.</p>
<p class="calibre8">The Mersenne Twister is an algorithm for generating random numbers. It was designed with consideration of the flaws in various other generators. The period, 2<sup class="calibre36">19937</sup>-1, and the order of equidistribution, 623 dimensions, are far greater. The generator is also fast; it avoids multiplication and division, and it benefits from caches and pipelines. For more information see the inventors' web page at http://www.math.keio.ac.jp/~matumoto/emt.html</p>
<p class="calibre8">Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, All rights reserved.</p>
<p class="calibre8">Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:</p>
<ol class="calibre19">
<li class="calibre11">Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.</li>
<li class="calibre11">Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.</li>
<li class="calibre11">The names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission.</li>
</ol>
<p class="calibre8">THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</p>
<p class="calibre8">Do NOT use for CRYPTOGRAPHY without securely hashing several returned values together, otherwise the generator state can be learned after reading 624 consecutive values.</p>
<p class="calibre8">When you use this, send an email to: matumoto@math.keio.ac.jp with an appropriate reference to your work. It would be nice to CC: rjwagner@writeme.com and Cokus@math.washington.edu when you write.</p>
<p class="calibre8"><i class="calibre21">This above has been already been done for AutoHotkey, but if you use the Random command in a publicly distributed application, consider sending an e-mail to the above people to thank them for their efforts and generosity.</i></p>

</div>
<p class="calibre8"> </p>
</div>
</div>
Expand Down Expand Up @@ -11028,7 +11031,7 @@ <h2 id="Examples">Examples</h2>
</tr>
<tr class="calibre3">
<td class="calibre4">explore</td>
<td class="calibre4">Opens an instance of Explorer at the indicated folder. Example: Run, explore %ProgramFiles%. <ahk class="x11">You can do that, but folders are being opened just fine without this verb, so it's unnecessary.</ahk> </td>
<td class="calibre4">Opens an instance of Explorer at the indicated folder. Example: Run, explore %ProgramFiles%. <span class="x11">You can do that, but folders are being opened just fine without this verb, so it's unnecessary.</span> </td>
</tr>
<tr class="calibre3">
<td class="calibre4">edit</td>
Expand Down
23 changes: 23 additions & 0 deletions src/cmd/math/random.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require "random"
# Random, OutputVar [, Min, Max]
class Cmd::Math::Random < Cmd::Base
def self.min_args; 1 end
def self.max_args; 3 end

def run(thread, args)
out_var = args[0]
min = args[1]? || ""
max = args[2]? || ""
as_int = ! min.includes?('.') && ! max.includes?('.')
if as_int
min = min.to_i? || 0
max = max.to_i? || Int32::MAX
value = ::Random.new.rand(min..max)
else
min = min.to_f? || 0_f64
max = max.to_f? || Int32::MAX.to_f
value = ::Random.new.rand(min..max)
end
thread.runner.set_user_var(out_var, value.to_s)
end
end

0 comments on commit c9f78bd

Please sign in to comment.