v1/v2 Random for both

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

v1/v2 Random for both

13 Aug 2018, 02:56

I want to write a library that is compatible with both v1 and v2.
Sadly I have encountered a problem that does not seem easy to solve towards me.

In my library I have to use the Random Command/Function to do something.
It is absolutely required and there is no way around (while I can just ignore v2 completely in my library)
It is possible to add code for v2 that does not cause issues when run under AHK v1.
Same can't be said for AHK v2. AHK v2s aggressive error checking finds that I pass too many parameters to the Random function:

Code: Select all

if isFunc("Random") {
	var := Random(min, max)
} else {
	Random var, min, max
}
This happens immediately at startup.

The next best solution is 2 different versions for v2 and v1 however Id rather neglect v2 compatability.

I also thought about using #Include with an A_ variable that is different for v1 and v2 like A_AhkVersion.
Then I would have to change the name of said include for each minor AHK version and add a special include for each version (not a good idea).

My last thought was about using a built in variable thats present in v2 and not v1 or vice-versa.
But as it turns out there is not a single AHK variable that matches my criterias and also has a defined static value at startup.

So my question is if any of you know how to make this possible.
Recommends AHK Studio
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: v1/v2 Random for both

13 Aug 2018, 03:56

I guess this would work,

Code: Select all

; lib\random.ahk 
Random(min, Max){
    Local 
    Random r, min, Max 
    Return r
}
it will be auto included in v1 because the function doesn't exist, in v2 it will not. To seed, do similar.

Cheers.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: v1/v2 Random for both

13 Aug 2018, 04:09

I see didn't think about that. Thank you
Recommends AHK Studio
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: v1/v2 Random for both

13 Aug 2018, 07:49

OK I jumped to conclusions way too easily here.
Sadly this is not an option. I want to write a library and that means that the library is in a different folder than the actual script that uses the library.
Since the lib folder needs to be in the folder of the main script I would have to move my code around - I dont want that.

Any other ideas?
Recommends AHK Studio
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: v1/v2 Random for both

13 Aug 2018, 09:23

Code: Select all

f(min, max){
	static r := func("random")
	static r_min := -2147483648.0
	static r_max := 2147483647.0
	local
	if  !r  {	; v1
		random out, r_min
		; [r_min, r_max] -> [min, max]
		out := (out - r_min) * (max - min) / (r_max-r_min) + min
		int_res := 0
		if min is integer
			int_res++
		if max is integer
			int_res++
		if (int_res == 2) 
			out := round(out)
	} else {	; v2
		out := r.call(min, max)
	}
	return out
}
Only use two parameters for v1, and normalise the result, double check I didn't make mistake.

Cheers.

Edit: I remember random do not have those limits for float numbers.
Last edited by Helgef on 13 Aug 2018, 11:00, edited 1 time in total.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: v1/v2 Random for both

13 Aug 2018, 09:31

Sadly this won't work for changing the seed.
And that is required.
Recommends AHK Studio
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: v1/v2 Random for both

13 Aug 2018, 09:34

Then you need to #include *i random.ahk for v1 users, or find an api function to do random numbers.

I have no other ideas.

Cheers
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: v1/v2 Random for both

13 Aug 2018, 09:56

Yeah I will probably have to implement a seperate way of generating random numbers.
Recommends AHK Studio
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: v1/v2 Random for both

13 Aug 2018, 10:59

This will be easy to solve if v2 can get a reference to built in functions even when a function with the same name has been defined in the script. Pseudo code,

Code: Select all

random(p*){
	static rs := func("randomseed")
	static r := func( (rs ? ["random", builtin_only := true] : [""])* )
	if !r {
		if (p[1] == "") {
			random ,, p[3] 				; no error in v2 due to redefinition
			return
		} else {
			random out ,p[1], p[2]
			return out
		}
	} else {
		if (p[1] == "")
			return rs.call(p[3])
		return r.call(p[1], p[2])
	}	
}
I think it is mentioned in v2 thought, like base.builtinfunc(...) or something like that (I do not like that particular idea though.)

Cheers.

edit:
v2-thoughts - classes wrote:If base is reserved everywhere, it could be used for calling an overridden built-in function (such as base.WinMove(x, y) in a redefinition of WinMove).
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: v1/v2 Random for both

13 Aug 2018, 11:24

Yeah that would be another option.
Recommends AHK Studio
guest3456
Posts: 3462
Joined: 09 Oct 2013, 10:31

Re: v1/v2 Random for both

13 Aug 2018, 14:44

nnnik wrote:Yeah I will probably have to implement a seperate way of generating random numbers.
this is best
you could investigate the AHK source for what Random does, and do the DllCalls directly in your script to replicate

thats basically what we've done in the AHKv2-GDIP to replace the SysGet/MonitorGet stuff. We use just-me's MDMF library which does the DllCalls
Last edited by guest3456 on 13 Aug 2018, 14:49, edited 1 time in total.

User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: v1/v2 Random for both

13 Aug 2018, 14:49

Well AHK uses a custom random number generator thats unrelated to any system API calls.
I wouldnt look forward to rebuilding that in MCode - though I wouldn't mind too much.
Recommends AHK Studio
guest3456
Posts: 3462
Joined: 09 Oct 2013, 10:31

Re: v1/v2 Random for both

13 Aug 2018, 14:52

nnnik wrote:Well AHK uses a custom random number generator thats unrelated to any system API calls.
I wouldnt look forward to rebuilding that in MCode - though I wouldn't mind too much.
there may be a winapi call you can try:

https://crypto.stackexchange.com/questi ... tropy-from
https://stackoverflow.com/questions/372 ... andom-apis

source code:
https://github.com/Microsoft/Windows-cl ... ration.cpp
https://github.com/Microsoft/Windows-dr ... il.cpp#L30

User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: v1/v2 Random for both

13 Aug 2018, 15:00

Sadly this wont allow me to set a seed.

But those are good links thank you.
Recommends AHK Studio
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: v1/v2 Random for both

13 Aug 2018, 15:10

https://github.com/Lexikos/AutoHotkey_L ... ar-cok.cpp

it's a lot less complex than I thought. I might be able to easily clone this into MCode.
Recommends AHK Studio
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: v1/v2 Random for both

13 Aug 2018, 16:21

- So eval is insufficient?
eval (using JS/COM) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=15389
- If you wanted a change to AHK as a long-term solution, perhaps you would consider using a command dynamically for AHK v1 and v2.
- Is any function as tough to recreate as Random? I'd had a look at the source code previously and it's pretty tough to recreate. I suppose RegEx would be another one.
- Although workarounds are often possible, I think it's time to consider something more substantial, what with guest3456, nnnik, and I, and no doubt others facing these issues. And the fact that some small source code solution could be relatively simple to implement, we just need to come to a collective decision that the main dev(s) are happy with.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: v1/v2 Random for both

13 Aug 2018, 18:13

No eval is unsufficient. Too slow, makes the user require IE.
Additionally none of the javascript generators allow for seeding.
I would have to import javascript code from somewhere to create a random number generator so that I could use it in eval for use in AHK.
I could just take this part of the AHK source and use it with almost no changes to create a new MCode method and it requires less work and is less annoying (since my project has to deal with MCode anyways).
To be honest I would rather write a new random generator in AHK than using one that has been connected from IE javascript over COM.
Recommends AHK Studio
User avatar
TheDewd
Posts: 1510
Joined: 19 Dec 2013, 11:16
Location: USA

Re: v1/v2 Random for both

11 Sep 2018, 11:27

jeeswg wrote:- So eval is insufficient?.
I think I used Eval on a project once, and I found that it wasn't supported for 64-bit AutoHotkey.
User avatar
Alguimist
Posts: 428
Joined: 05 Oct 2015, 16:41
Contact:

Re: v1/v2 Random for both

12 Sep 2018, 06:52

Some time ago I posted a code for CryptGenRandom, but since I moved to a new computer it no longer works (NTE_BAD_PUBLIC_KEY).

But I found some alternatives:
rand_s

Code: Select all

rand_s(Seed, Min := 0, Max := 0XFFFFFFFF) {
    DllCall("msvcrt.dll\rand_s", "UInt*", Seed)
    Return Mod(Seed, (Max + 1 - Min)) + Min
}

n := ""
Loop 1000 {
    n .= rand_s(A_TickCount, 0, 100) . " "
}
MsgBox % n
RtlRandomEx

Code: Select all

RtlRandom(Seed, Min := 0, Max := 0x7FFFFFFFFFFFFFFE) {
    Return Mod(DllCall("ntdll.dll\RtlRandomEx", "UInt*", Seed), (Max + 1 - Min)) + Min
}

n := ""
Loop 1000 {
    n .= RtlRandom(A_TickCount * A_Index, 0, 100) . " "
}

MsgBox % n

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Giresharu, Google [Bot], haomingchen1998, Thorlian and 291 guests