Random Number: Mersenne Twister Source Needed

This is a discussion on Random Number: Mersenne Twister Source Needed within the C++ Programming forums, part of the General Programming Boards category; I was researching different random number generation techniques for use in the generation of a random integer nonce. While researching, ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    88

    Random Number: Mersenne Twister Source Needed

    I was researching different random number generation techniques for use in the generation of a random integer nonce. While researching, I found that the Mersenne Twister is often referred to as one of the fastest and best random number generators. I had a few questions.

    1.) Will it allow me to generate any value over the range of 32-bit signed integers?

    2.) Is there any reason using the Mersenne Twister would be a poor choice for generating my nonces?

    3.) And most importantly, is there any thoroughly tested Mersenne Twister source code available for c++ that should work with the VC++ 6.0 compiler? I am looking for code that is free for commercial use.

    Thank you for any information you can provide.

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    MSVC++ 6.0 is an early compiler system. I personally have not heard of the mersenne twister, but Im guessing it is some sort of function that generates random numbers. Older compilers may implement it, ie: MSVC++6.0 or Turbo C++ but newer ones may not. Do a google on the internet too, you may find out somthing about it.

    Thats all I can offer im afraid, others might give better advise. Good luck
    I'm just trying to be a better person - My Name Is Earl

  3. #3
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeĺ, Sweden
    Posts
    555
    1.) Will it allow me to generate any value over the range of 32-bit signed integers?
    There's one version using 32 bit integers and one using 64.
    3.) And most importantly, is there any thoroughly tested Mersenne Twister source code available for c++ that should work with the VC++ 6.0 compiler? I am looking for code that is free for commercial use.
    Google

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,535
    I personally use the R. Wagner's implementation found somewhere on the original MT inventors website.

    It's very fast and with several nice additions like generator state save and restore, user-defined or default real or integer ranges, inclusive and exclusive ranges and several seed generation methods, from system clock to /dev/urandum. All wrapped inside a documented class for easy of use.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    88
    Swgh: Thanks for your info.

    OnionKnight: Thanks for the info. I have found source code searching, but I am looking for an implementation that is reputable. I was hoping to find someone who has experience with particular code and knows it is thoroughly tested and a solid implementation.

    Edit: Sorry you posted as I was making this post.

    Mario F: Thank you as well. Would you happen to have a link to that implementation? Is it free for use in commercial products?

    Thanks to everyone for all the info.
    Last edited by mercury529; 11-25-2006 at 08:52 PM.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,535
    R. Wagner's or Coku, from what I know. I prefer the first.

    EDIT: Oh. I forgot Prelude's as suggested by Dave. Very easy to use too. And will fit your needs if you don't need to repeat results.
    Last edited by Mario F.; 11-25-2006 at 08:48 PM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,179
    EDIT: Oh. I forgot Prelude's as suggested by Dave. Very easy to use too. And will fit your needs if you don't need to repeat results.
    hmm... doesn't Prelude's implementation have a seed function too?
    Last edited by laserlight; 11-26-2006 at 10:48 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    88
    Thank you everyone for the responses. I appreciate them.

    I tried the code provided in the link by Dave.

    Two quick questions. Is the code on that page free for use in commercial products? I was unable to find any information on the site to that extent. I want to make sure I am allowed to use the code. Secondly, it seems to me like jsw_seed must be called before any call to jsw_rand. Is that correct?

    Thanks again everyone.

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,535
    Quote Originally Posted by laserlight
    hmm... doesn't Prelude's implementation have a seed function too?
    Yup. Here, unsigned jsw_time_seed(). The result can then be stored and used as an argument to jsw_seed(). I didn't remember about this one. My bad.

    Quote Originally Posted by mercury529
    Is the code on that page free for use in commercial products?
    Check the code initial comment block.

    > Secondly, it seems to me like jsw_seed must be called before any call to jsw_rand. Is that correct?

    Yup. It suffices if you do it just one time. you can call jsw_seed() passing it a unsigned long literal, or better yet, doing this:

    Code:
    jsw_seed( jsw_time_seed() );
    Just do yourself a favor and alter the code so that anywhere you read 'unsigned' without a type being specified immediatly after it, you have instead 'unsigned int'. int is no longer the default type in C++.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    88
    Mario: Thanks for the info.

    I checked directly before and after the MT code section. Is that what you were referring to? I was looking for something that condoned its use in commercial products specifically just to be sure I was not infringing upon anyone's copyrighted materlal. Am I looking in the right place?

    Also, in the MT code provided (jsw_seed and jsw_rand being the two functions I am using) I did not find any instances of unsigned without a type given. Did I miss an instance or was that just a general warning given other code on the page?

    Thanks again.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,179
    I checked directly before and after the MT code section. Is that what you were referring to? I was looking for something that condoned its use in commercial products specifically just to be sure I was not infringing upon anyone's copyrighted materlal. Am I looking in the right place?
    Dave_Sinkula linked to Prelude's random number article, which touches on the Mersenne Twister. The code that you should use is the one from her Random Number Library.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    88
    laser: Thank you. That clears it all up.

    Thanks again to everyone who helped me with my questions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random number problems
    By lespaul5895 in forum C Programming
    Replies: 3
    Last Post: 07-05-2007, 03:16 AM
  2. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 04:33 AM
  3. Counting number from a random file
    By kamisama in forum C Programming
    Replies: 42
    Last Post: 02-22-2005, 05:16 PM
  4. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 03:40 PM
  5. random number
    By mrukok in forum C++ Programming
    Replies: 7
    Last Post: 03-16-2003, 08:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21