Hi everyone... I have code in three different files. I would like you to have a look at it.
MersenneTwister.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#ifndef MERSENNETWISTER_H
#define MERSENNETWISTER_H
#include <stdlib.h>
#define SZ_STATE 624
#define KNUTH_MUL 1812433253
typedef unsigned long long UINT64;
typedef unsigned int UINT32;
typedef unsigned short int UINT16;
UINT64 * initializeGenerator (UINT32);
UINT64 extractNumber(UINT16);
UINT64 * generateNumbers(UINT64 *);
UINT64 * randStream(UINT64);
UINT64 randNumber(UINT64);
#endif /* MERSENNETWISTER_H */
|
MersenneTwister.c
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
|
#include "MersenneTwister.h"
/*-----------------------------------------------------------------------------
Create a new generator state vector and fill it with Knuth's feedback shift
register algorithm
-----------------------------------------------------------------------------*/
UINT64 * initializeGenerator (UINT32 Seed) {
UINT64 * generatorState = NULL; // Define placeholder for generator state
if (!generatorState) { // Just checking...
generatorState = (UINT64 *) malloc (SZ_STATE * sizeof(UINT64)); // Allocate memory
if (!generatorState) return NULL; // If not possible, return NULL to indicate FAILURE
}
generatorState[0] = (UINT64) Seed; // Cast 32-bit seed to 64-bit placeholder and store
UINT16 Count; for (Count=1; Count<SZ_STATE; Count++) { // From [1] to [623] fill state vector with Knuth's
generatorState[Count] =
((KNUTH_MUL * ((generatorState[Count-1]) ^
((generatorState[Count-1])>>30)) + Count) << 32) >> 32; // Rotate MT[i-1] 30-right, XOR with MT[i-1], multiply
} // Knuth's multiplier, add i, truncate to 32-bits
return generatorState;
}
unsigned long ExtractNumber(unsigned short Index){
}
unsigned long * GenNumbers(unsigned long * currentState){
}
unsigned long * RandStream(unsigned long Seed){
}
unsigned long RandNumber(void){
}
|
main.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include <stdio.h>
#include <stdlib.h>
#include "MersenneTwister.h"
int main() {
UINT64 *State;
State = initializeGenerator(62742);
UINT16 n; for(n=0;n<SZ_STATE;n++){
printf("%u: %llx\n", n, State[n]);
}
return (EXIT_SUCCESS);
}
|
In case anyone is still wondering, I'm trying to write a very basic implementation of Mersenne Twister (
http://en.wikipedia.org/wiki/Mersenne_twister). Now, the implementation is not complete, but the
initializeGenerator
was somewhat complete. So I thought I'd look at the state vector (an array of 64-bit integers) that it generates.
I'm compiling with
gcc 4.6.3. It compiles fine. No errors, no warnings. When I execute it, the output is not right.
When I run it in GNOME terminal, it runs fine, output 624 number, one on each line. When I run it in Netbeans terminal, it sometimes runs 200 something times and exits with code 0 and sometimes runs 495 times and sticks, no indication of successful or unsuccessful exit.
It may be a bug in Netbeans, but I'm not sure I'm doing it right. Could you answer a few questions after having a look at the code?
- Why is the output not right in Netbeans terminal? Do you spot a serious blunder somewhere in there?
- If it indeed is a problem with Netbeans terminal, can you spot some examples of bad programming practice in my code? You know, something that may be unsafe or not recommended?
- How would you do it? |
Thank you for your time, gentlemen.