how to link random number generator with cpu?

This is a discussion on how to link random number generator with cpu? within the C++ Programming forums, part of the General Programming Boards category; // randomnumber.cpp: implementation of the randomnumber class. // ////////////////////////////////////////////////////////////////////// #include "randomnumber.h" #include <stdlib.h> #include <ctime> #include <iostream> using namespace std; ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    84

    how to link random number generator with cpu?

    // randomnumber.cpp: implementation of the randomnumber class.
    //
    //////////////////////////////////////////////////////////////////////

    #include "randomnumber.h"
    #include <stdlib.h>
    #include <ctime>
    #include <iostream>

    using namespace std;

    //////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////

    randomnumber::randomnumber()
    {

    }

    randomnumber::~randomnumber()
    {

    }

    randomnumber::getnumber()
    {
    return randnumber;
    }

    randomnumber::generatenumber()
    {
    randnumber = ( rand () % 20 ) +1;
    return 0;
    }

    i have this file which generates a random number. how to i link it into the cpu clock cycle so it is even more random? i know its supposed to go in the 'randomnumber::randomnumber()
    ' bit. how do i do this?

  2. #2
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    I'm not sure if I undestood your question well! but I think you might be referring to this:
    Code:
    srand(time(0));
    This will randomize the generation of random number...

    Remeber: you only need to include this code in your program once.
    none...

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    84
    sounds about right, thanks for the input.

    i may be asking lots of questions over the next 2 days as i have a uni assignment to do.

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    84
    u put that bit of code in the class constructor, and when compiling the file i got stuck in a infinte loop. any ideas?

  5. #5
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    You don't even need to seed, but it is always a good idea.

    Code:
    #include <iostream>
    #include <cstdlib>
    
    int main()
    {
    	for (int i=0; i < 10; i++)
    		std::cout << rand() % 10;
    	return 0;
    }
    1740948824
    EDIT: The compiler was stuck in a loop or your program was?
    Last edited by Munkey01; 04-28-2003 at 05:09 AM.

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    84
    it was the compiler i think. it was still compiling after about 5min, and i couldn't exit the prog as it was still building.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random number gen & looping probelm :?
    By FoxeySoulSLayer in forum C++ Programming
    Replies: 1
    Last Post: 04-10-2009, 05:44 PM
  2. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  3. random number problems
    By lespaul5895 in forum C Programming
    Replies: 3
    Last Post: 07-05-2007, 02:16 AM
  4. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  5. random number
    By mrukok in forum C++ Programming
    Replies: 7
    Last Post: 03-16-2003, 07:04 PM

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