Random Number

This is a discussion on Random Number within the C++ Programming forums, part of the General Programming Boards category; How can I make a function, or if there is already a function that will return a different random number ...

  1. #1
    56k
    56k is offline
    Registered User
    Join Date
    Feb 2006
    Posts
    1

    Random Number

    How can I make a function, or if there is already a function that will return a different random number each time I use it.

    Like rand(1,10); will return a random number 1-10.

    And if I start up the program again, or use the function again it won't return the same number.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Check the FAQ all the way down at the bottom under the heading Prelude's corner.

  3. #3
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    Code:
    #include <stdlib.h>
    #include <time.h>
    
    int randnum (int start, int end)
    {
        static bool notseeded = true;
    
        if (notseeded) {
            srand(time(0));
            notseeded = false;
        }
    
        return rand()%(end-start+1) + start;
    }

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