Thread: Show random number

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    3

    Question Show random number

    I have a game that i am trying to add to, learning what the functions do when i change them. The game is Jackpot which comes with Dev-C++ compiler, the player has 5 votes and i was wondering if there is a way to show the random number after the guesses are up.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Read the FAQ.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Post Try this...

    I think this is what you need. Try storing the random number as a global (After the #include files). Example:

    Code:
    #include <myfile.h>
    #include <file2.h>
    // Other includes
    
    int nRand = 0;    // Random number
    I hope this helps.

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146

    Re: Try this...

    Originally posted by SyntaxBubble
    I think this is what you need. Try storing the random number as a global (After the #include files)...I hope this helps.
    I fail to see how telling someone to declare a global variable helps anything.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  5. #5
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Code:
    #include <ctime>
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int random(int min, int max);
    
    int main()
    {
      srand(time(NULL));
      cout << rand(6,12);     // Prints a random number between 6 and 12
      return 0;
    }
    
    int random(int min, int max)
    {
      int random = rand() % (max - min);
      random += min;
      return random;
    }
    I just threw this together, hasn't been tested, should do the job.

  6. #6
    Registered User LogicError's Avatar
    Join Date
    Aug 2003
    Location
    г. Магнитогорск
    Posts
    76
    Originally posted by Brian
    Code:
    #include <ctime>
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int random(int min, int max);
    
    int main()
    {
      srand(time(NULL));
      cout << rand(6,12);     // Prints a random number between 6 and 12
      return 0;
    }
    
    int random(int min, int max)
    {
      int random = rand() % (max - min);
      random += min;
      return random;
    }
    I just threw this together, hasn't been tested, should do the job.
    Maybe I need a pair of glasses, but i think you're wrong there..
    Code:
      cout << rand(6,12);
    should be
    Code:
      cout << random(6,12);
    Correct me if I'm wrong, I'm just really sleepy and it's late here, so I can't tell myself :P

  7. #7
    Registered User LogicError's Avatar
    Join Date
    Aug 2003
    Location
    г. Магнитогорск
    Posts
    76
    This already been posted: here

    If you got a question like that, just put 'random' in the search box, it'll help a lot and spare us all some time

  8. #8
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    ok i got your solution here, if you don't want to click search, and im bored anyway:
    Code:
    /*Lucas Campbell turned the original mt19937 into a header file */
    
    //Here are the original Credits:
    /* A C-program for MT19937: Real number version                */
    /*   genrand() generates one pseudorandom real number (double) */
    /* which is uniformly distributed on [0,1]-interval, for each  */
    /* call. sgenrand(seed) set initial values to the working area */
    /* of 624 words. Before genrand(), sgenrand(seed) must be      */
    /* called once. (seed is any 32-bit integer except for 0).     */
    /* Integer generator is obtained by modifying two lines.       */
    /*   Coded by Takuji Nishimura, considering the suggestions by */
    /* Topher Cooper and Marc Rieffel in July-Aug. 1997.           */
    
    /* This library is free software; you can redistribute it and/or   */
    /* modify it under the terms of the GNU Library General Public     */
    /* License as published by the Free Software Foundation; either    */
    /* version 2 of the License, or (at your option) any later         */
    /* version.                                                        */
    /* This library is distributed in the hope that it will be useful, */
    /* but WITHOUT ANY WARRANTY; without even the implied warranty of  */
    /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.            */
    /* See the GNU Library General Public License for more details.    */
    /* You should have received a copy of the GNU Library General      */
    /* Public License along with this library; if not, write to the    */
    /* Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA   */ 
    /* 02111-1307  USA                                                 */
    
    /* Copyright (C) 1997 Makoto Matsumoto and Takuji Nishimura.       */
    /* Any feedback is very welcome. For any question, comments,       */
    /* see http://www.math.keio.ac.jp/matumoto/emt.html or email       */
    /* [email protected]                                        */
    
    
    #ifndef MT_RAND_H
    #define MT_RAND_H
    
    #define N 624
    #define M 397
    #define MATRIX_A 0x9908b0df   /* constant vector a */
    #define UPPER_MASK 0x80000000 /* most significant w-r bits */
    #define LOWER_MASK 0x7fffffff /* least significant r bits */
    
    /* Tempering parameters */   
    #define TEMPERING_MASK_B 0x9d2c5680
    #define TEMPERING_MASK_C 0xefc60000
    #define TEMPERING_SHIFT_U(y)  (y >> 11)
    #define TEMPERING_SHIFT_S(y)  (y << 7)
    #define TEMPERING_SHIFT_T(y)  (y << 15)
    #define TEMPERING_SHIFT_L(y)  (y >> 18)
    
    static unsigned long mt[N]; /* the array for the state vector  */
    static int mti=N+1; /* mti==N+1 means mt[N] is not initialized */
    void sgenrand(unsigned long seed) /*unsigned long seed;	*/
    {
        /* 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]= seed & 0xffffffff;
        for (mti=1; mti<N; mti++)
            mt[mti] = (69069 * mt[mti-1]) & 0xffffffff;
    }
    
    
    
    //double /* generating reals */
    extern unsigned long  /* for integer generation */
    genrand()
    {
        unsigned long y;
        static unsigned long mag01[2]={0x0, MATRIX_A};
        /* mag01[x] = x * MATRIX_A  for x=0,1 */
    
        if (mti >= N) { /* generate N words at one time */
            int kk;
    
            if (mti == N+1)   /* if sgenrand() has not been called, */
                sgenrand(4357); /* a default initial seed is used   */
    
            for (kk=0;kk<N-M;kk++) {
                y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
                mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1];
            }
            for (;kk<N-1;kk++) {
                y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
                mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1];
            }
            y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
            mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1];
    
            mti = 0;
        }
      
        y = mt[mti++];
        y ^= TEMPERING_SHIFT_U(y);
        y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B;
        y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C;
        y ^= TEMPERING_SHIFT_L(y);
    
        //return ( (double)y / (unsigned long)0xffffffff ); /* reals */
        return y;  /* for integer generation */
    }
    #endif
    
    
    
    /* end mtRand.h */
    use this extremely... interesting and speedy way of generating random numbers, (works pretty well for modulo 10)

    to do so
    Code:
    sgenrand(time(NULL)); //make sure you included time.h
    int myRand = genrand() % 10; //from 0-9
    I hope this has made your life extremely more difficult in the process ^_^

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  9. #9
    Registered User LogicError's Avatar
    Join Date
    Aug 2003
    Location
    г. Магнитогорск
    Posts
    76
    couldnt you just:

    Code:
    #include <stdlib.h>
    #include <time.h>
    .
    .
    .
      srand(time(0));  // Set random seed
      int myRand = rand()%10;  // Generate number from 0-9
    .
    .
    .
    Much simpler..

  10. #10
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    ah yes you could, but the point is to make it insanely difficult so the person gets aggrivated and reads the FAQ.

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  11. #11
    Registered User LogicError's Avatar
    Join Date
    Aug 2003
    Location
    г. Магнитогорск
    Posts
    76
    Yeah, you made it insanely difficult for me too, but I wouldnt read the FAQ anyway, I'd just copy your function, put it in a header and use it without knowing how it works

  12. #12
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    it's not MY function ^_^

    and go ahead and copy it and use it as you like but if you turn it in as homework your teacher will most definately look at you funny. And if you do use it make sure you copy the comments at the top. This is copyrighted material and you are allowed to redistribute edit etc. as long as you give credit where it is due. The two brilliant guys who did this and me the lowly programmer that stays up at late hours who had nothing better to do than implement a header file with the functions they used (and it wasn't as simple as you'd think it would be, I actually had to spend a little bit of time on it because statics kept disapperaing out of no where -_-)


    also I'm going to quote Knuth on this (well it was from his book at least)

    Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin.
    --John Van Neumann (1951)

    -LC
    Last edited by Lynux-Penguin; 08-31-2003 at 02:34 PM.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  13. #13
    Registered User LogicError's Avatar
    Join Date
    Aug 2003
    Location
    г. Магнитогорск
    Posts
    76
    If i had homework i'd just use the rand() anyway

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 10:46 AM
  2. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  3. How exactly does the random number generator work?
    By Finchie_88 in forum C++ Programming
    Replies: 6
    Last Post: 08-24-2007, 12:46 AM
  4. Counting number from a random file
    By kamisama in forum C Programming
    Replies: 42
    Last Post: 02-22-2005, 05:16 PM
  5. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM