How to program multiple pseudo-random generators in AHK

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
hurrestara
Posts: 3
Joined: 24 Jun 2017, 07:01

How to program multiple pseudo-random generators in AHK

10 Jul 2017, 08:24

Hello

I need to generate multiple pseudo-random sequences, all independent of each other.
I am using different randomizer values in different functions

Function1(integerSeed1) {
Random, , %integerSeed1%
; .... other code
Random, sequencyValue1, minValue, maxValue
Random, sequencyValue2, minValue, maxValue
Random, sequencyValue3, minValue, maxValue
; etc...
}

Function2(integerSeed2) {
Random, , %integerSeed2%
; .... other code
Random, sequencyValue1, minValue, maxValue
Random, sequencyValue2, minValue, maxValue
Random, sequencyValue3, minValue, maxValue
; etc...
}

Function3(integerSeed3) {
Random, , %integerSeed3%
; .... other code
Random, sequencyValue1, minValue, maxValue
Random, sequencyValue2, minValue, maxValue
Random, sequencyValue3, minValue, maxValue
; etc...
}

I want to obtain that all sequences generated by each function (Function1, Function2, Function3, etc.) be identical to a previous run of the SAME function.

This works ok as long as each run for a given function, say FunctionX, is not overtaken by a run of another FunctionY called within the code of FunctionX.
This is obvious since each function reinitializes the unique pseudo-random generator of AHK with another seed value and on returning to the calling function
the generator is in another state (re-initializing it with the corresponding integer of the calling function does not work because the generator restarts
the sequency from the beginning again).

Has anyone a clue on how to create independent pseudo-random generators in AHK?

I hope there is another solution than launching different simultaneous AHK processes.

hurrestara
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: How to program multiple pseudo-random generators in AHK

10 Jul 2017, 09:15

Just out of curiosity - for what is such a routine used for?? :wtf:
hurrestara
Posts: 3
Joined: 24 Jun 2017, 07:01

Re: How to program multiple pseudo-random generators in AHK

12 Jul 2017, 05:40

Responding to Helgef:
I just cannot imagine how the Critical statement would solve the reseeding problem. What I need is to continue a given DETERMINISTIC sequence (initiated, say with 'integerSeed1') after returning from another called function that RESEEDED the generator (say with 'integerSeed2'). I would need to recover the generator's STATE from the first calling function, at the moment prior to calling the other function, so that after the other function completion, the generator continues generating from the STATE as it was before (as if the other reseeding function was not called). But AHK does not offer means to obtain the generato's state, nor to force the generator to generate from a previous registered state.
In other languages, you can instantiate several random number generators, running simultaneously in separate threads, and each one with it's own independent seed. So, every random generator does not interfere with the states of the other generators.

If you could provide some coding example to achieve this feat with the Critical statement, I would be very grateful.
Thanks for your answer anyway.

Responding to BoBo:
It's a quick prototype for a gaming application. I thought that coding with AHK would be helpful for a proof of concept. In clear, I am analyzing the behaviour of some automata (represented by independent threads) when subject to random sequences of input events. In some cases, these events depend on the actions performed by another automaton which is also behaving according to its own detrerministic sequence of events. I cannot say more for confidentiality reasons.
Programmer's curiosity meets limits when business is at stake ;-)

hurrestara
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: How to program multiple pseudo-random generators in AHK

12 Jul 2017, 06:00

Hi hurrestara.
My point was to use critical to avoid,
This works ok as long as each run for a given function, say FunctionX, is not overtaken by a run of another FunctionY called within the code of FunctionX.
This would work unless you explicitly call FunctionY from FunctionX, I figured the functions where interrupting each other via, eg, SetTimer.
If you want the same sequences, just precalculate them, eg

Code: Select all

Function1() {
	static sequency:=rand(n,minValue,maxValue,integerSeed1)
	ind:=0
	; other code
	sequencyValue1:=sequency[++ind]
	sequencyValue2:=sequency[++ind]
	sequencyValue3:=sequency[++ind]
; etc...
}
Function2() {
	static sequency:=rand(n,minValue,maxValue,integerSeed2)
	; ...
}
rand(n,a,b,seed:=""){
	if (seed!="")
		Random,,% seed
	r:=[]
	r.SetCapacity(n)
	Loop, % n {
		random,rnd,a,b
		r.push(rnd)
	}
	return r
}
Edit: ofc, you'd have know n which might not be trivial.
Edit2: I do not know c# so I do not give any guarantees on this one :lol:

Code: Select all

c# =
(
using System;
public class rnd {
	
	private Random rng;	
	public void seed(Int32 seed){
		rng = new Random(seed);
	}
	public int gen(Int32 lb, Int32 ub){
		return rng.Next(lb,ub);
	}
}
)
asm := CLR_CompileC#(c#, "System.dll")
rnd := CLR_CreateObject(asm, "rnd")
rnd2 := CLR_CreateObject(asm, "rnd")
rnd.Seed(1)
rnd2.Seed(1)
loop, 5
	r1.=rnd.gen(1,10) "`n" 
loop, 5
	r2.=rnd2.gen(1,10) "`n" 
msgbox, % r1 "`n---`n" r2
You need CLR by lexikos. See Random Class.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: hiahkforum and 105 guests