Mersenne Twister Seed

Oh man, can anybody help me get a working version of the mersenne twister going? i have tried about three or four different implementations now, including direct c++ ports of the original C and versions in C all kinds, i can run all the samples supplied with these implementations and see lots of stacks of floating point numbers, but i CANNOT get any of them to do what you want, ie give me a whole number from range n to n and SEED the blasted thing so i get different output each time!

I tried the seed by array, seed with 0, seed with bloody sunflower seeds, i just get same output every time.

Can somebody describe how to use the major seed functions that you see with the Mersenne twister?

Cheers all.



Last edited on
You can just initialize the array with a simple lcg algo or something similar, which you seed with the seed intended for the mersenne twister.
You generate a number in a certain range with mersenne_rand()%(max-min+1)+min;
So something like this example to generate a seed value:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
 
 #define M 2147483647
 #define A 16807
 #define Q ( M / A )
 #define R ( M % A )
 
 static int seed = 1;
 
int jsw_rand ( void )
{
  seed = A * ( seed % Q ) - R * ( seed / Q );

  if ( seed <= 0 )
    seed += M;

  return seed;
}

int main ( void )
{
  int i;

  for ( i = 0; i < 10; i++ )
    printf ( "%d ", jsw_rand() );
  printf ( "...\n" );

  return 0;
}


Then i put this into my chosen function from the mersenne twister..

How do i get a common garden variety integer returned?, casting? the only thing these functions seem to deal in is long unsigned ints and what have you

EDIT: The value returned from this function is the same every time, so how is that of any use?
Last edited on
Heres a version i tried, same output every time. although i now have the range sorted, thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<stdlib.h>
#include <cstdio>
#include "mtrand.h"

#define M 2147483647
#define A 16807
#define Q ( M / A )
#define R ( M % A )

static int seed = 1;

int jsw_rand ( void )
{
  seed = A * ( seed % Q ) - R * ( seed / Q );

  if ( seed <= 0 )
    seed += M;

  return seed;
}


int main()
{
    unsigned long temp = 0;
    int seed = jsw_rand();
    int max = 20;
    int min = 0;

    printf("SEED %d\n", seed);


    MTRand_int32 rand_num;
    rand_num.seed(seed);

    for (int i = 0; i < 10; ++i)
    {
        temp = rand_num()%(max - min+1) + min;

        printf("%1lu ", temp);
        if ((i % 5) == 4) std::printf("\n");
    }

  return 0;
}
Last edited on
Do not use #define where not necessary.
If you're using the same seed every time, you'll obviously get the same number sequence each time.
Use something that constantly changes as a seed, i.e. time(0).
Topic archived. No new replies allowed.