   Random number generator
  What is the code on c++ for a random number generator?   
  Mersenne Twister comes with C++ source.   ;  Do you mean, is there a random number generator in the Standard Library for C++? The answer to that question is yes. Check out std::rand. You also need std::srand to seed the generator.If, however, your question was asking for someone to post code for a random number generator, then ignore what I just wrote! 8)Cheers,Timkin   ;  yes i would like the code for it   ;Quote:Original post by bxela1yes i would like the code for itHeh. You still didn't clarify your question.  What does 'it' refer to?You would like the code for what?  Do you want the code for how to use rand() or do you want the code of the rand() function itself?-me   ;  I need to use a RNG for a text-based game im making.I dont know which code im supposed to have   ;#include <iostream>#include <cstdlib>#include <ctime>using namespace std;int main(){srand(time(0)); //seed random number generatorint randomNumber = rand(); //generate random numberint die = ( randomNumber % 6 ) + 1; //get number between one & sixcout << "You rolled " << die << endl;return 0}The above simulates a dice roll, generating a number between one and six. It's lifted from Michael Dawson's book Beginning C++ Game Programming, which I can't recommend enough if you're just starting out like me. Rather than explaining it I recommend you test it, fiddle with it, google every command & generally get to understand every last little detail. Get a copy of a good book such as the one I mentioned. By the way, the "%" means "modulus" (google it!).Hope this helps,Sven   ;  This is how I do it, but there are other ways also.#include <ctime> //Include this to use 'time()'srand(time(NULL)); //Set up the generaterint MyNumber;int main(){   MyNumber = (rand() % 50) + 1; //Creates a number between 0 and 49, adds 1 to it, to make it 1 - 50}You can only use an int; no bools, floats, or other varaibles.[Edit:]Heh, I'm to slow.   ;#include <iostream>#include <conio.h>#define P(t) std::cout << t#define I(v) std::cin >> vtypedef struct{    private:    int rand_seed;    public:    void seed(int s)    {   rand_seed = s;    }  int generate(int max)    {   if (rand_seed <= 0) rand_seed = 2;   rand_seed = rand_seed * 1103515245 +12345;   return abs(((rand_seed / 65536) % 32768)%max);    }}RandomGenerator;int main(){    int i;    RandomGenerator rgen;    rgen.seed(5);    for(i = 0; i < 5; i++)    {  P(rgen.generate(9));    }    getch();    return 0;}P.S. Please don't flame me for not making struct RandomGenerator a class and having a constructor to init rand_seed. I was bored so I tried out a struct [smile].[Edited by - AcePilot on May 24, 2006 8:16:28 PM];Quote:Original post by tabyMersenne Twister comes with C++ source.On that note this is from my Game Coding Complete book which is a must read for any game programmer out there by the way://========================================================================// CMath_Random.cpp -  a useful random number generator class//// Part of the GameCode2 Application//// GameCode2 is the sample application that encapsulates much of the source code// discussed in "Game Coding Complete - 2nd Edition" by Mike McShaffry, published by// Paraglyph Press. ISBN: 1-932111-91-3//// If this source code has found it's way to you, and you think it has helped you// in any way, do the author a favor and buy a new copy of the book - there are // detailed explanations in it that compliment this code well. Buy a copy at Amazon.com// by clicking here: http://www.amazon.com/exec/obidos/ASIN/1932111913/gamecodecompl-20///// There's also a companion web site at http://www.mcshaffry.com/GameCode/portal.php//// (c) Copyright 2005 Michael L. McShaffry//// This work is licensed under the Creative Commons Attribution-ShareAlike License. // To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/1.0/ // or send a letter to:// Creative Commons// 559 Nathan Abbott Way// Stanford, California 94305, USA.////========================================================================//========================================================================//  Content References in Game Coding Complete 2nd Edition// //========================================================================////  "Mersenne Twister pseudorandom number generator" //  Original Code written by Takuji Nishimura and Makoto Matsumoto////========================================================================//========================================================================//  Content References in Game Coding Complete 2nd Edition// // class CRandom- Chapter 3, pages 76-79////========================================================================#include "GameCodeStd.h"#include <time.h>#include "CMath.h"/////////////////////////////////////////////////////////////////////////////// DEBUG info///////////////////////////////////////////////////////////////////////////////--------------------------------------------------------------------------------CRandom::CRandom(void){rseed = 1;// safe0 startrseed_sp = 0;mti=CMATH_N+1;// safe0 end} // Returns a number from 0 to n (excluding n)unsigned int CRandom::Random( unsigned int n ){    unsigned long y;    static unsigned long mag01[2]={0x0, CMATH_MATRIX_A};if(n==0)return(0);    /* mag01[x] = x * MATRIX_A  for x=0,1 */    if (mti >= CMATH_N) { /* generate N words at one time */   int kk;   if (mti == CMATH_N+1)   /* if sgenrand() has not been called, */  SetRandomSeed(4357); /* a default initial seed is used   */   for (kk=0;kk<CMATH_N-CMATH_M;kk++) {  y = (mt[kk]&CMATH_UPPER_MASK)|(mt[kk+1]&CMATH_LOWER_MASK);  mt[kk] = mt[kk+CMATH_M] ^ (y >> 1) ^ mag01[y & 0x1];   }   for (;kk<CMATH_N-1;kk++) {  y = (mt[kk]&CMATH_UPPER_MASK)|(mt[kk+1]&CMATH_LOWER_MASK);  mt[kk] = mt[kk+(CMATH_M-CMATH_N)] ^ (y >> 1) ^ mag01[y & 0x1];   }   y = (mt[CMATH_N-1]&CMATH_UPPER_MASK)|(mt[0]&CMATH_LOWER_MASK);   mt[CMATH_N-1] = mt[CMATH_M-1] ^ (y >> 1) ^ mag01[y & 0x1];   mti = 0;    }y = mt[mti++];    y ^= CMATH_TEMPERING_SHIFT_U(y);    y ^= CMATH_TEMPERING_SHIFT_S(y) & CMATH_TEMPERING_MASK_B;    y ^= CMATH_TEMPERING_SHIFT_T(y) & CMATH_TEMPERING_MASK_C;    y ^= CMATH_TEMPERING_SHIFT_L(y);// ET - old engine added one to the result.// We almost NEVER wanted to use this function// like this.  So, removed the +1 to return a // range from 0 to n (not including n).    return (y%n);}// Returns a random float between 0.0f-1.0f// NEW for Game Code Complete 2nd Edition!float CRandom::Random( ){int r = (float)Random(0xffffffff);float divisor = (float)0xffffffff;return r / divisor;}void CRandom::SetRandomSeed(unsigned int n){/* setting initial seeds to mt[N] using    *//* the generator Line 25 of Table 1 in*//* [KNUTH 1981, The Art of Computer Programming *//*    Vol. 2 (2nd Ed.), pp102]   */mt[0]= n & 0xffffffff;for (mti=1; mti<CMATH_N; mti++)mt[mti] = (69069 * mt[mti-1]) & 0xffffffff;rseed = n;}unsigned int CRandom::GetRandomSeed(void){return(rseed);}void CRandom::Randomize(void){SetRandomSeed((unsigned int)time(NULL));}//--------------------------------------------------------------------------------// EOF