how to generate a random number in c++ ?

Discussion in 'C++' started by b.t.e, Jan 1, 2008.

  1. b.t.e

    b.t.e New Member

    Joined:
    Jan 1, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    A supermarket wants to test how well their store will function with different number of cashiers serving customers. Write a program that simulates that situation using queues. First, your program should ask how many cashiers will be serving customers. You should make sure that the number entered is between 1 and 5. For each simulated minute you should generate a random number of buyers (between 0 and 3), each buyer takes from 2 to 6 minutes to be served (generate the number of service minutes randomly). Customers are served on a first-come first serve basis. A customer will stay with a cashier until the time required has elapsed, then the cashier can serve the next customer. The simulation ends when no cashiers are busy and no people enter the store.

    how to generate a random number in c++ ?
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <time.h>
    
    using namespace std;
    
    int main()
    {
    	//	Declare variable to hold seconds on clock.
    	time_t seconds;
    	//Get value from system clock and place in seconds variable.
    	time(&seconds);
    	// 	Convert seconds to a unsigned integer.
    	srand((unsigned int) seconds);
    	// Output random values.
    	cout<< rand() << endl;
    	cout<< rand() << endl;
    	cout<< rand() << endl;
    	return 0;
    }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice