Results 1 to 13 of 13
  1. #1
    SpiderByte's Avatar
    Join Date
    Jan 2006
    Posts
    103
    Reputation
    10
    Thanks
    3

    Random Integer Generator

    Code:
    //Program 2.5 - Using Random Integers
    include<iostream>    //Input/Output Stream
    #include<cstdlib>    //CstdLibrary
    #include<ctime>      //CTime
    using std::cin;      //Define cin
    using std::cout;     //Define cout
    using std::endl;     //Define endl;
    using std::rand;     //Define rand;
    using std::srand;    //Define srand;
    using std::time;     //Define time;
    
    int main(){                    //Initiate function main()
        const int limit1 = 500;    //Upper limit for one set of random values
        const int limit2 = 31;     //Upper limit for another set of values
        
        cout << "First we will use the default sequnce from rand().\n";  //Output
        cout << "Three random integers from 0 to " << RAND_MAX << ": "   //Integer from 0 to 32767
             << rand() << " " << rand() << " " << rand() << endl;        //Random integers
             
        cout << endl << "Now we will use a new seed for rand().\n";      //Output
        srand((unsigned int)time(0));    //Set a new seed
        
        cout << "Three random integers from 0 to " << RAND_MAX << ": "   //Integer from 0 to 32767
             << rand() << " " << rand() << " " << rand() << endl;        //Random integers
             
        cout << endl << "Please press the return (enter) key to exit the program . . .";  //Output
        cin.get();     //Wait for keypress(enter)
        return 0;      //Return successful
        
    }

  2. #2
    OutZida's Avatar
    Join Date
    Dec 2005
    Gender
    male
    Posts
    172
    Reputation
    15
    Thanks
    72
    What is it?
    What does it do?

  3. #3
    Flawless's Avatar
    Join Date
    Dec 2005
    Gender
    male
    Location
    Brink Of Sanity
    Posts
    625
    Reputation
    129
    Thanks
    478
    Yes I'm curious about this too..

  4. #4
    arunforce's Avatar
    Join Date
    Dec 2005
    Gender
    male
    Location
    A place for amigos
    Posts
    24,700
    Reputation
    4747
    Thanks
    12,561
    My Mood
    Yeehaw
    ROFL, it generates an integer (number).
    READ THE TITLE, DAMMIT. >.<



    BRING BACK BT, BRING BACK SAGA, BRING BACK VF, BRING BACK MPGHCRAFT, BRING BACK HABAMON


  5. #5
    Flawless's Avatar
    Join Date
    Dec 2005
    Gender
    male
    Location
    Brink Of Sanity
    Posts
    625
    Reputation
    129
    Thanks
    478
    Yeah, but what can it be used for?

  6. #6
    kyo's Avatar
    Join Date
    Dec 2005
    Location
    Belgium
    Posts
    285
    Reputation
    110
    Thanks
    131
    not a lot of uses. REALY basic...

  7. #7
    Flawless's Avatar
    Join Date
    Dec 2005
    Gender
    male
    Location
    Brink Of Sanity
    Posts
    625
    Reputation
    129
    Thanks
    478
    Then why is it in the Sig section?

  8. #8
    arunforce's Avatar
    Join Date
    Dec 2005
    Gender
    male
    Location
    A place for amigos
    Posts
    24,700
    Reputation
    4747
    Thanks
    12,561
    My Mood
    Yeehaw
    It isn't in Sigs, ROFL.



    BRING BACK BT, BRING BACK SAGA, BRING BACK VF, BRING BACK MPGHCRAFT, BRING BACK HABAMON


  9. #9
    kyo's Avatar
    Join Date
    Dec 2005
    Location
    Belgium
    Posts
    285
    Reputation
    110
    Thanks
    131
    it used to be, i moved it

  10. #10
    shercipher's Avatar
    Join Date
    Dec 2005
    Posts
    108
    Reputation
    10
    Thanks
    14
    Derrrr...it generates a random integer.

    EDIT:
    Quel nub. This code is shorter and works also (note the C leetness of this code).

    Code:
    #include <stdlib.h>
    #include <time.h>
    #define RAND_MAX 500
    
    int main() {
    srand(time_t);
    return rand(RAND_MAX);
    }
    /* alternatively you can #include <stdio.h> and printf the rand int */
    At least that is what I remember.


    Oh and by the way what was with all this?
    Code:
    using std::cin;      //Define cin
    using std::cout;     //Define cout
    using std::endl;     //Define endl;
    using std::rand;     //Define rand;
    using std::srand;    //Define srand;
    using std::time;     //Define time;
    Look at this - I can combine all of that into one line:

    Code:
    using namespace std;
    And I get all the std:: uberleetness!

  11. #11
    Dave84311's Avatar
    Join Date
    Dec 2005
    Gender
    male
    Location
    The Wild Wild West
    Posts
    35,837
    Reputation
    5782
    Thanks
    41,292
    My Mood
    Devilish
    Quote Originally Posted by shercipher
    Derrrr...it generates a random integer.
    Look at this - I can combine all of that into one line:

    Code:
    using namespace std;
    And I get all the std:: uberleetness!
    Not exactly C ne more





    THE EYE OF AN ADMINISTRATOR IS UPON YOU. ANY WRONG YOU DO IM GONNA SEE, WHEN YOU'RE ON MPGH, LOOK BEHIND YOU, 'CAUSE THATS WHERE IM GONNA BE


    "First they ignore you. Then they laugh at you. Then they fight you. Then you lose.” - Dave84311

    HAVING VIRTUAL DETOX

  12. #12
    shercipher's Avatar
    Join Date
    Dec 2005
    Posts
    108
    Reputation
    10
    Thanks
    14
    bah, the C only applied to that leet program, or logram. This was just a way not to have to state every single function/stream you are using.

  13. #13
    SpiderByte's Avatar
    Join Date
    Jan 2006
    Posts
    103
    Reputation
    10
    Thanks
    3
    Generating Random Numbers

    Being able to generate random numbers in a program is very useful. You need to be able to build randomness into game programs, for instance; otherwise, they become boring very quickly. The <cstblib> header defines a function rand() that will generate random integers. To be more precise, it generates pseudo-random integers. Random numbers by definition aren't predictable, so any sequence of numberss produced by a numberical algorithm can't truely be random. It just has the appearance of being so. However, now that you understand that, you'll just refer to the numbers produced by the rand() function as random numbers. Note that rand() isn't actually an outstanding random number generator. For many applications, you'll probably want to use something that's rather more sophisticated.
    The rand() function returns a random integer as type int. The function doesn't require any arguments, so you can just use it like this:

    int random_value = std:rand(); // A random integer

    Your store the integer that's returned by the rand() function here in the variable random_value, but you could equally well use it in an arithmetic expression, for example:

    int even = 2*std::rand();

    The value returned by the rand() function will be a value that is from 0 to RAND_MAX. RAND_MAX is a symbol that is defined in <cstdlib>. When you use RAND_MAX in your code, the compiler will replace it with an integer value. On my system it represents the value 0x7fff, but on other systems it may have a different value. It can be up to 0x3fffffff, which is the maximum integer you can store in type int. If this was the case, then you couldn't multiply the value produced by rand() by 2, as you did previously, without running the risk of getting an incorrect result.
    Because RAND_MAX is defined by a preprocessing macro, it isn't within the std namespace, so you don't need to qualify the name when you use it. Any symbol that's defined by a macro won't be in the std namespace because it isn't a name that refers to something. By the time the compiler gets to compile the code, such a symbol will no longer be present because it will have already been replaced by something else during the preprocessing phase.



    Making the Sequence Start at Random

    Using rand() as you have so far, the sequence of numbers will always be the same. This is because the function uses a default seed value in the algorithm that generates the random numbers. This is fine for testing, but once you have a working game program, you'll really want different sequences each time the program runs. You can change the seed value that will be used to generate the numbers by passing a new seed value as an integer argument to the srand() function that is defined in <cstdlib>, for example:

    std::srand(13); // Set seed for rand to 13

    The argument to the srand() function must be a value of type unsigned int. Although the preceding statement will result in rand() generating a different sequence from the default, you really need a random seed to get a different sequence from rand() each time you execute a program. Fortunately, the clock on your computer provides a ready-made source of random seed values.
    The <ctime> standard library header defines several functions relating to the data and the time. You'll just look at the time() function here because that's precisely what you need to obtain a more or less random seed value. The time() function returns a value that's the number of seconds that have elapsed since midnight on January 1, 1970, so if you use that as a seed, you can be certain that a program will use a different seed value each time it executes. The value is returned as type time_t, which is a type defined in the standard library to be equivalent to an integer type, usually type long. The return type is specified as type time_t to allow felxability in the type of the return value in different C++ implementations. You can use the time() function to create the seed for a random number sequence like this:

    std::srand((unsigned int )std::time(0));

    There are a few things that you'll have to take on trust for the moment. The argument to the time() function here is 0. There's another possibility for the argument, but you don't need it here so you'll ignore it. The subexpression (unsigned int) serves to convert the value returned by the time() function to type unsigned int, which is the type required for the argument to the srand() method. Without this, the statement wouldn't compile.
    Let's put a working example together that makes use of random number generation.

    Code:
    //Program 2.5 - Using Random Integers
    #include<iostream>
    #include<cstdlib>
    #include<cmath>
    using std::cin;
    using std::cout;
    using std::endl;
    using std::rand;
    using std::srand;
    using std::time;
    // These all can be done by simpley using the using namepsace std, but is better to do it this way in small programs, as the using namespace std can sometimes conflict with your code.
    
    int main(){
        const int limit1 = 500;  // Upper limit for one set of random values
        const int limit2 = 31;   // Upper limit for another set of values
    
        cout << "First we will use the default sequence from rand().\n";
        cout << "Three random integers from 0 to " << RAND_MAX << ": "
               << rand() << " " << rand() << " " << rand() << endl;
    
        cout << endl
               << "Now we will use a new seed for rand().\n";
        srand((unsigned int)time(0));  // Set a new seed
    
        cout << "Three random integers from 0 to " << RAND_MAX << ": "
               << rand() << " " << rand() << " " << rand() << endl;
    
        cin.get();
    
        return 0;
    }
    On my system I get the following output:

    Code:
    First we will use the defualt sequence from rand().
    Three random integers from 0 to 32767: 6344 18467 41 // These will always be the same
    
    Now we will use a new seed for rand().
    Three random integers from 0 to 32767: 4610 32532 28452 // These will always be different
    Don't try to make me look like I don't know what I'm doing...

Similar Threads

  1. Random Integer Error? I think?
    By gwentravolta in forum Visual Basic Programming
    Replies: 9
    Last Post: 09-08-2009, 04:52 AM
  2. [Application] Random key generator
    By youngbucks in forum Visual Basic Programming
    Replies: 17
    Last Post: 03-24-2009, 11:09 AM
  3. [Release]Random Username Generator
    By Lipit in forum Combat Arms Hacks & Cheats
    Replies: 21
    Last Post: 08-27-2008, 08:21 AM
  4. Random Number Generator
    By Iam"iDude" in forum Visual Basic Programming
    Replies: 5
    Last Post: 10-02-2007, 04:16 PM
  5. Random Integer Generator
    By SpiderByte in forum Art & Graphic Design
    Replies: 6
    Last Post: 01-22-2006, 09:51 AM