C++ question........random numbers
Results 1 to 6 of 6

Thread: C++ question........random numbers

  1. #1
    Registered User ±Octavian's Avatar
    Join Date
    Sep 2000
    Location
    Michigan
    Posts
    289

    Post C++ question........random numbers

    Before I start this a part of a homework assignment. I need to genrate random numbers, 100 of them. I am not sure if this a function that I must write or if C++ has something like this already. So main question is it a function or is it reseved command? If it is a function could some one help me with the algorithm of the fuction. If it is coded pleeas just point me to the right place. Note that if I recieve help From anyone on this there email address will be part of my comments due to professors requirements.

    Q if this classifies as cheating please tell me or delete this post.
    Faith is the substance of things not known and the Evidence of things not seen.

  2. #2
    Registered User Deity's Avatar
    Join Date
    Mar 2001
    Location
    Elsewhere
    Posts
    1,412

    Post

    C++ has a built in random function. the function is called "rand". Unfortunately I don't remember the exact syntax and have none of my reference materials nearby. This rand function may be part of a header file, possibly stdlib.h. Rand actually returns a floating point value between 0 and 1. In order to get random numbers in a specific range, such as 1 to 100, extra code is required. The rand function however, will generate the same psuedo random numbers each time the code is executed. There is another function called srand that creates a random number seed, changing the values that rand produces.

    100 random numbers: //code may not be 100% correct, this is from memory

    int iMyInt = 0;
    int iHigh = 100 //highest possible random number
    int iLow = 0 //lowest possible random number
    for (int iLoop = iLow, iLoop < iHigh, ++iLoop) {
    iMyInt = rand * (iHigh - iLow) + iLow //generates random number & stores in myint
    cout << iMyInt << endl;
    }

    Again, I stress that this code is from memory, and may not be entirely accuate. The functions rand and srand are correct though.

    Good Luck
    A bored admin is a very dangerous person...

  3. #3
    Registered User
    Join Date
    Jan 2001
    Location
    Aurora, IL
    Posts
    340

    Post

    I looked around some for you and found a good page the should help you with this here
    There should also be referances of this function in almost any C++ programming book.

  4. #4
    Registered User ±Octavian's Avatar
    Join Date
    Sep 2000
    Location
    Michigan
    Posts
    289

    Post

    Originally posted by mahdi:
    I looked around some for you and found a good page the should help you with this here
    There should also be referances of this function in almost any C++ programming book.
    Thanks bud from what I have seen from that website that is pehaps the best one I have seen. I will email you the code when I get it finished.

    Thanks Deity as well. You both have been a great help.

  5. #5
    Registered User migel628's Avatar
    Join Date
    Jul 2000
    Location
    New York
    Posts
    99

    Post

    I usually use the randomize(x) command. I believe that is actually C though. If I can find an example using that, then I'll post it.

  6. #6
    Registered User
    Join Date
    Apr 2001
    Location
    Raleigh,NC
    Posts
    89

    Post

    Whoa, slow down. It is much easier than that. Here is what you have to do. First, include the time.h file. That is Microsoft so just do #include&lt;time.h&gt;. Then at the very top of your int main or void main put srand((unsigned)time(NULL)); that initializes randoms. Then anytime you want a random number, say I want int random to be a random number, I would do random = (rand() %10) +1; where the number after the modulus(%), in this case 10, is what you want your number to be max. This will make a number 1-10. Now an example:

    // your headers here
    #include&lt;time.h&gt;

    int main()
    {
    srand((unsigned)time(NULL));
    int random;
    // your program here
    //now you want a random say 1-6 you will do:
    random = (rand() %6) +1;
    //the rest of your program
    return 0;
    }//end main function

    That is all!! If you want further explaination email me at [email protected]. I am happy to answer all C++ questions that I can.
    If at first you dont succeed, sit down and take a break.
    (Breaking things is not good!)

    Hardware Notes - http://www.hardwarenotes.com

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •