## Translate C to Ahk Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Prototyp
Posts: 21
Joined: 02 May 2017, 14:43
Location: Deutschland NRW

### Translate C to Ahk

Hi, how i can translate the Original Mersennen Twister Code to ahk?

Code: Select all

``````
/* A C-program for TT800 : July 8th 1996 Version */
/* by M. Matsumoto, email: [email protected] */
/* genrand() generate one pseudorandom number with double precision */
/* which is uniformly distributed on [0,1]-interval */
/* for each call.  One may choose any initial 25 seeds */
/* except all zeros. */

/* See: ACM Transactions on Modelling and Computer Simulation, */
/* Vol. 4, No. 3, 1994, pages 254-266. */

#include <stdio.h>
#define N 25
#define M 7

double
genrand()
{
unsigned long y;
static int k = 0;
static unsigned long x[N]={ /* initial 25 seeds, change as you wish */
0xc1de75b7, 0x8858a9c9, 0x2da87693, 0xb657f9dd, 0xffdc8a9f,
};
static unsigned long mag01={
0x0, 0x8ebfd028 /* this is magic vector `a', don't change */
};
if (k==N) { /* generate N words at one time */
int kk;
for (kk=0;kk<N-M;kk++) {
x[kk] = x[kk+M] ^ (x[kk] >> 1) ^ mag01[x[kk] % 2];
}
for (; kk<N;kk++) {
x[kk] = x[kk+(M-N)] ^ (x[kk] >> 1) ^ mag01[x[kk] % 2];
}
k=0;
}
y = x[k];
y ^= (y << 7) & 0x2b5b2500; /* s and b, magic vectors */
y ^= (y << 15) & 0xdb8b0000; /* t and c, magic vectors */
y &= 0xffffffff; /* you may delete this line if word size = 32 */
/*
the following line was added by Makoto Matsumoto in the 1996 version
to improve lower bit's corellation.
Delete this line to o use the code published in 1994.
*/
y ^= (y >> 16); /* added to the 1994 version */
k++;
return( (double) y / (unsigned long) 0xffffffff);
}

/* this main() output first 50 generated numbers */
main()
{ int j;
for (j=0; j<50; j++) {
printf("%5f ", genrand());
if (j%8==7) printf("\n");
}
printf("\n");
}

``````
Rohwedder
Posts: 3381
Joined: 04 Jun 2014, 08:33
Location: Germany

### Re: Translate C to Ahk

Hallo,
use https://autohotkey.com/docs/commands/Random.htm
This function uses the Mersenne Twister random number generator, MT19937, written by Takuji Nishimura and Makoto Matsumoto, Shawn Cokus, Matthe Bellew and Isaku Wada.
Prototyp
Posts: 21
Joined: 02 May 2017, 14:43
Location: Deutschland NRW

### Re: Translate C to Ahk

Rohwedder wrote:Hallo,
use https://autohotkey.com/docs/commands/Random.htm
This function uses the Mersenne Twister random number generator, MT19937, written by Takuji Nishimura and Makoto Matsumoto, Shawn Cokus, Matthe Bellew and Isaku Wada.

i know the function. But I need the code what works in ahk.
stealzy
Posts: 82
Joined: 01 Nov 2015, 13:43

### Re: Translate C to Ahk

HotKeyIt
Posts: 2139
Joined: 29 Sep 2013, 18:35
Contact:

### Re: Translate C to Ahk

Try this:

Code: Select all

``````Loop 50
out.=genrand() "`n"
MsgBox % out

genrand(){
static k = 1, N = 25, M:=7
;/* initial 25 seeds, change as you wish */
static x:=[0x95f24dab, 0x0b685215, 0xe76ccae7, 0xaf3ec239, 0x715fad23,	0x24a590ad, 0x69e4b5ef, 0xbf456141, 0x96bc1b7b, 0xa7bdf825,	0xc1de75b7, 0x8858a9c9, 0x2da87693, 0xb657f9dd, 0xffdc8a9f,	0x8121da71, 0x8b823ecb, 0x885d05f5, 0x4e20cd47, 0x5a9ad5d9,	0x512c0c03, 0xea857ccd, 0x4cc1d30f, 0x8891a8a1, 0xa6b7aadb]
static mag01:=[0x0, 0x8ebfd028] ; /* this is magic vector `a', don't change */
if (k==N) { ; /* generate N words at one time */
Loop % n-m
x[kk:=A_Index] = x[A_Index+M] ^ (x[A_Index] >> 1) ^ mag01[mod(x[A_Index],2)]
Loop % N-kk
x[kk+A_Index] := x[kk+A_Index+(M-N)] ^ (x[kk+A_Index] >> 1) ^ mag01[mod(x[A_Index], 2)]
k:=1
}
y := x[k]
y ^= (y << 7) & 0x2b5b2500 ; /* s and b, magic vectors */
y ^= (y << 15) & 0xdb8b0000 ; /* t and c, magic vectors */
y &= 0xffffffff ; /* you may delete this line if word size = 32 */
y ^= (y >> 16) ; /* added to the 1994 version */
k++
return y / 0xffffffff
}``````
Prototyp
Posts: 21
Joined: 02 May 2017, 14:43
Location: Deutschland NRW

### Re: Translate C to Ahk

HotKeyIt wrote:Try this:

Code: Select all

``````Loop 50
out.=genrand() "`n"
MsgBox % out

genrand(){
static k = 1, N = 25, M:=7
;/* initial 25 seeds, change as you wish */
static x:=[0x95f24dab, 0x0b685215, 0xe76ccae7, 0xaf3ec239, 0x715fad23,	0x24a590ad, 0x69e4b5ef, 0xbf456141, 0x96bc1b7b, 0xa7bdf825,	0xc1de75b7, 0x8858a9c9, 0x2da87693, 0xb657f9dd, 0xffdc8a9f,	0x8121da71, 0x8b823ecb, 0x885d05f5, 0x4e20cd47, 0x5a9ad5d9,	0x512c0c03, 0xea857ccd, 0x4cc1d30f, 0x8891a8a1, 0xa6b7aadb]
static mag01:=[0x0, 0x8ebfd028] ; /* this is magic vector `a', don't change */
if (k==N) { ; /* generate N words at one time */
Loop % n-m
x[kk:=A_Index] = x[A_Index+M] ^ (x[A_Index] >> 1) ^ mag01[mod(x[A_Index],2)]
Loop % N-kk
x[kk+A_Index] := x[kk+A_Index+(M-N)] ^ (x[kk+A_Index] >> 1) ^ mag01[mod(x[A_Index], 2)]
k:=1
}
y := x[k]
y ^= (y << 7) & 0x2b5b2500 ; /* s and b, magic vectors */
y ^= (y << 15) & 0xdb8b0000 ; /* t and c, magic vectors */
y &= 0xffffffff ; /* you may delete this line if word size = 32 */
y ^= (y >> 16) ; /* added to the 1994 version */
k++
return y / 0xffffffff
}``````

Works Perfect.             Prototyp
Posts: 21
Joined: 02 May 2017, 14:43
Location: Deutschland NRW

### Re: Translate C to Ahk       Something is wrong. the script ist repeating the pseudo numbers

The Script will visualize the problem. execute it.

for bench with the internal gen remove ;

Code: Select all

``````Gui, Show,  h800 w670, New GUI Window

process, Exist
pid_this := ErrorLevel

WinGet, hw_canvas, ID, ahk_class AutoHotkeyGUI ahk_pid %pid_this%

hdc_canvas := DllCall( "GetDC", "uint", hw_canvas )
DllCall( "BitBlt", "uint", hdc_canvas, "int", 10, "int", 20, "int", 650, "int", 750, "uint", hdc_buffer, "int", 0, "int", 0, "uint", 0x00FF00FF )

y=20

loop,750
{
loop, 50
{
x :=genrand()
; random,x, 1, 650 ; Activate this for bench
x+=10
DllCall( "gdi32.dll\MoveToEx", "uint", hdc_canvas, "int", x, "int", y, "uint", lppoint)
DllCall( "gdi32.dll\LineTo", "uint", hdc_canvas, "int", x+=1, "int", y)
if a_index = 50
y++
}
}

genrand(){
static k = 1, N = 25, M:=7
;/* initial 25 seeds, change as you wish */
static x:=[0x95f24dab, 0x0b685215, 0xe76ccae7, 0xaf3ec239, 0x715fad23,	0x24a590ad, 0x69e4b5ef, 0xbf456141, 0x96bc1b7b, 0xa7bdf825,	0xc1de75b7, 0x8858a9c9, 0x2da87693, 0xb657f9dd, 0xffdc8a9f,	0x8121da71, 0x8b823ecb, 0x885d05f5, 0x4e20cd47, 0x5a9ad5d9,	0x512c0c03, 0xea857ccd, 0x4cc1d30f, 0x8891a8a1, 0xa6b7aadb]
static mag01:=[0x0, 0x8ebfd028] ; /* this is magic vector `a', don't change */
if (k==N) { ; /* generate N words at one time */
Loop % n-m
x[kk:=A_Index] = x[A_Index+M] ^ (x[A_Index] >> 1) ^ mag01[mod(x[A_Index],2)]
Loop % N-kk
x[kk+A_Index] := x[kk+A_Index+(M-N)] ^ (x[kk+A_Index] >> 1) ^ mag01[mod(x[A_Index], 2)]
k:=1
}
y := x[k]
y ^= (y >> 11)
y ^= (y << 7) & 0x9D2C5680
y ^= (y << 15) & 0xEFC60000
y ^= (y >> 18)
k++
;y := (1 + ( y * ( 70 - (1 + 1))) / (1<<32) )
;y := y / 0xffffffff
;y := y * (1.0/4294967296.0) ; = 4294967295
y := mod( y,  ((650 - 1 + 1) + 1))
;msgbox, %y%
return y ;(y>>32) ; / 0xffffffff
}

return

guiclose:
exitapp
``````
Last edited by Prototyp on 19 May 2017, 11:40, edited 2 times in total.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

### Re: Translate C to Ahk

x[kk:=A_Index] := x[A_Index+M] ^ (x[A_Index] >> 1) ^ mag01[mod(x[A_Index],2)]
Prototyp
Posts: 21
Joined: 02 May 2017, 14:43
Location: Deutschland NRW

### Re: Translate C to Ahk

kon wrote:x[kk:=A_Index] := x[A_Index+M] ^ (x[A_Index] >> 1) ^ mag01[mod(x[A_Index],2)]
nope, this makes the Problem bigger
HotKeyIt
Posts: 2139
Joined: 29 Sep 2013, 18:35
Contact:

### Re: Translate C to Ahk  Topic is solved

Sorry, there were 2 errors Code: Select all

``````genrand(){
static k = 1, N = 25, M:=7
;/* initial 25 seeds, change as you wish */
static x:=[0x95f24dab, 0x0b685215, 0xe76ccae7, 0xaf3ec239, 0x715fad23,	0x24a590ad, 0x69e4b5ef, 0xbf456141, 0x96bc1b7b, 0xa7bdf825,	0xc1de75b7, 0x8858a9c9, 0x2da87693, 0xb657f9dd, 0xffdc8a9f,	0x8121da71, 0x8b823ecb, 0x885d05f5, 0x4e20cd47, 0x5a9ad5d9,	0x512c0c03, 0xea857ccd, 0x4cc1d30f, 0x8891a8a1, 0xa6b7aadb]
static mag01:=[0x0, 0x8ebfd028] ; /* this is magic vector `a', don't change */
if (k==N) { ; /* generate N words at one time */
Loop % n-m
x[kk:=A_Index] := x[A_Index+M] ^ (x[A_Index] >> 1) ^ mag01[mod(x[A_Index],2)+1]
Loop % N-kk
x[kk+A_Index] := x[kk+A_Index+(M-N)] ^ (x[kk+A_Index] >> 1) ^ mag01[mod(x[A_Index], 2)+1]
k:=1
}
y := x[k]
y ^= (y >> 11)
y ^= (y << 7) & 0x9D2C5680
y ^= (y << 15) & 0xEFC60000
y ^= (y >> 18)
k++
;y := (1 + ( y * ( 70 - (1 + 1))) / (1<<32) )
;y := y / 0xffffffff
;y := y * (1.0/4294967296.0) ; = 4294967295
y := mod( y,  ((650 - 1 + 1) + 1))
;msgbox, %y%
return y ;(y>>32) ; / 0xffffffff
}``````
Prototyp
Posts: 21
Joined: 02 May 2017, 14:43
Location: Deutschland NRW

### Re: Translate C to Ahk

HotKeyIt wrote:Sorry, there were 2 errors Code: Select all

``````genrand(){
static k = 1, N = 25, M:=7
;/* initial 25 seeds, change as you wish */
static x:=[0x95f24dab, 0x0b685215, 0xe76ccae7, 0xaf3ec239, 0x715fad23,	0x24a590ad, 0x69e4b5ef, 0xbf456141, 0x96bc1b7b, 0xa7bdf825,	0xc1de75b7, 0x8858a9c9, 0x2da87693, 0xb657f9dd, 0xffdc8a9f,	0x8121da71, 0x8b823ecb, 0x885d05f5, 0x4e20cd47, 0x5a9ad5d9,	0x512c0c03, 0xea857ccd, 0x4cc1d30f, 0x8891a8a1, 0xa6b7aadb]
static mag01:=[0x0, 0x8ebfd028] ; /* this is magic vector `a', don't change */
if (k==N) { ; /* generate N words at one time */
Loop % n-m
x[kk:=A_Index] := x[A_Index+M] ^ (x[A_Index] >> 1) ^ mag01[mod(x[A_Index],2)+1]
Loop % N-kk
x[kk+A_Index] := x[kk+A_Index+(M-N)] ^ (x[kk+A_Index] >> 1) ^ mag01[mod(x[A_Index], 2)+1]
k:=1
}
y := x[k]
y ^= (y >> 11)
y ^= (y << 7) & 0x9D2C5680
y ^= (y << 15) & 0xEFC60000
y ^= (y >> 18)
k++
;y := (1 + ( y * ( 70 - (1 + 1))) / (1<<32) )
;y := y / 0xffffffff
;y := y * (1.0/4294967296.0) ; = 4294967295
y := mod( y,  ((650 - 1 + 1) + 1))
;msgbox, %y%
return y ;(y>>32) ; / 0xffffffff
}``````
i see no difference You're incredibly good K is the Position of the index right? , M and N is??
HotKeyIt
Posts: 2139
Joined: 29 Sep 2013, 18:35
Contact:

### Re: Translate C to Ahk

+1 was missing N is the size of Array, M is required to not exceed array size if I understand correct.
Prototyp
Posts: 21
Joined: 02 May 2017, 14:43
Location: Deutschland NRW

### Re: Translate C to Ahk

im back with a Little problem i try ad a own seed function, but he will not works.

Code: Select all

``````
seed()
{
SetFormat,Integer,h
loop, 25
{
r = 9
s := 999 * a_index
r := 509845221 * (r + 3)
s := s * (s + 1)
xy := s + (r >> 10)
if a_index < 25
xy .= ", "
xz .= xy
}
return xz
SetFormat,Integer,D
}
``````

Code: Select all

``````static x := "[" seed() "]"
``````

what is wrong?
HotKeyIt
Posts: 2139
Joined: 29 Sep 2013, 18:35
Contact:

### Re: Translate C to Ahk

Code: Select all

``````seed(){
xy:=[]
loop, 25
{
r = 9
s := 999 * a_index
r := 509845221 * (r + 3)
s := s * (s + 1)
xy.Push(s + (r >> 10))
}
return xy
}``````
Prototyp
Posts: 21
Joined: 02 May 2017, 14:43
Location: Deutschland NRW

### Re: Translate C to Ahk

You have to understand I'm a beginner. Thank you. Sorry for my bad english
Prototyp
Posts: 21
Joined: 02 May 2017, 14:43
Location: Deutschland NRW

### Re: Translate C to Ahk

Hmm, seed() is blank?
Helgef
Posts: 4459
Joined: 17 Jul 2016, 01:02
Contact:

### Re: Translate C to Ahk

Prototyp wrote:You have to understand I'm a beginner. Thank you. Sorry for my bad english
Prototyp wrote:Hmm, seed() is blank?
Do you know how to use a for loop to go through an array? Here is an example.

Code: Select all

``````xy:=seed()
str:=""
for key, value in xy
str.= key " = " value  "`n"
Msgbox, % str
``````
Prototyp
Posts: 21
Joined: 02 May 2017, 14:43
Location: Deutschland NRW

### Re: Translate C to Ahk

Helgef wrote:
Prototyp wrote:You have to understand I'm a beginner. Thank you. Sorry for my bad english
Prototyp wrote:Hmm, seed() is blank?
Do you know how to use a for loop to go through an array? Here is an example.

Code: Select all

``````xy:=seed()
str:=""
for key, value in xy
str.= key " = " value  "`n"
Msgbox, % str
``````
No but now 