random number

This is a discussion on random number within the C++ Programming forums, part of the General Programming Boards category; how can you have a random number generator in a function, and have it generate another random number each time ...

  1. #1
    mrukok
    Guest

    random number

    how can you have a random number generator in a function, and have it generate another random number each time the function is called within main()?

    (when i tried, it would generate a different random number when the program is restarted, but each time the function is called in the program, it gives the same number).

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Read the FAQ.

    And make sure that you only call srand( ) once, at the start of your program. If you have it within the function, it is rather pointless.
    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
    mrukok
    Guest
    but thats all in C

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by mrukok
    but thats all in C
    Generating the number is the same in C++. Just because it uses printf()'s and such, doesn't mean you can't swap them for their cout equivalents.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    mrukok
    Guest
    but im easily confused!

  6. #6
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Originally posted by mrukok
    but im easily confused!

    Code:
    #include <iostream.h>  
    #include <stdlib.h>  
    #include <time.h>  
    
    int main(void)
    {
      int i;
      
      srand(time(NULL));
      
      i = rand();
      
      cout << "Your random number is  << i << endl;  
    
      cout<< "This compiler can generate random numbers from 0 to " <<  RAND_MAX;
    
      return(0);
    }
    Code:
    #include <iostream.h> 
    #include <stdlib.h> 
    
    int GetRand(int min, int max);
    
    int main(void)
    {
      int i, r;
      
      for (i = 0; i < 20; i++)
      {
        r = GetRand(10, 12);
        cout <<  "Your number is " <<  r << ' ';
      }
      
      return(0);
    }
    
    int GetRand(int min, int max)
    {
      static int Init = 0;
      int rc;
      
      if (Init == 0)
      {
        /*
         *  As Init is static, it will remember it's value between
         *  function calls.  We only want srand() run once, so this
         *  is a simple way to ensure that happens.
         */
        srand(time(NULL));
        Init = 1;
      }
    
      /*
       * Formula:  
       *    rand() % N   <- To get a number between 0 - N-1
       *    Then add the result to min, giving you 
       *    a random number between min - max.
       */  
      rc = (rand() % (max - min + 1) + min);
      
      return (rc);
    }
    Or similar
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  7. #7
    mrukok
    Guest
    thanks
    .. i'll see how that works soon
    .. going to bed now, it 1/4 past 1 in the morning!

  8. #8
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>#include <iostream.h>
    >>#include <stdlib.h>
    >>#include <time.h>

    tsk tsk tsk

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rapid random number generation problem
    By Newton in forum C Programming
    Replies: 17
    Last Post: 09-19-2008, 03:08 PM
  2. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 10:46 AM
  3. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 01:56 PM
  4. random number tutorial
    By 7stud in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2005, 03:41 PM
  5. Random Number Generator
    By Ikurik in forum C++ Programming
    Replies: 16
    Last Post: 08-17-2003, 08:34 PM

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