Example #1
0
int main()
{
	srand( time(NULL) ); //seed random generator
	while( 1 )
	{
		int number1 = generateNumber(); // generate one number
		int number2 = generateNumber(); // generate second number

		int correctResult = -1;
		correctResult = number1 * number2; // correct multiplication result

		// display which numbers are being multiplyed
		// To exit loop, input 0
		displayMessage( number1, number2 );
		int userResult = -1; 				// user result of multiplication
		cin >> userResult;					// read input

		// exit program
		if( userResult == 0 )
			break;

		// repeat until result is correct
		while( userResult != correctResult )
		{
			displayAfterWrongAnswer();
			cin >> userResult;
		}
		
		// display message for correct result
		if( userResult == correctResult )
			displayAfterCorrectAnswer();
	}
	
	return 0;
}// end main function
Example #2
0
void generate_data(mpi::communicator local, mpi::communicator world)
{
  using std::srand;
  using std::rand;

  // The rank of the collector within the world communicator
  int master_collector = local.size();

  srand(time(0) + world.rank());

  // Send out several blocks of random data to the collectors.
  int num_data_blocks = rand() % 3 + 1;
  for (int block = 0; block < num_data_blocks; ++block) {
    // Generate some random data
    int num_samples = rand() % 1000;
    std::vector<int> data;
    for (int i = 0; i < num_samples; ++i) {
      data.push_back(rand());
    }

    // Send our data to the master collector process.
    std::cout << "Generator #" << local.rank() << " sends some data..."
              << std::endl;
    world.send(master_collector, msg_data_packet, data);
  }

  // Wait for all of the generators to complete
  (local.barrier)();

  // The first generator will send the message to the master collector
  // indicating that we're done.
  if (local.rank() == 0)
    world.send(master_collector, msg_finished);
}
Example #3
0
/*
	Purpose: To shuffle the cards in the Deck, can be more than once but will be 
			 Less than or equal to the MAX_SHUFFLE count, prevents from shuffling 
			 the Deck 4 billion times.

	Entry: The number of times to shuffle the Deck

	Exit: The cards in the deck have now been placed in a random order
*/
void Deck::Shuffle(int TimesToShuffle)
{
	cout << "Will shuffle " << TimesToShuffle << " times." << endl;
	if((TimesToShuffle > 0) && (TimesToShuffle <= MAX_SHUFFLE))
	{
		int iCount = 0;

		while(iCount < TimesToShuffle)
		{
			for(int i = 0; i < MAX_NUMBER_OF_CARDS; i++)
			{
				srand(static_cast<int>(time(0)));
				int RandomNumber = rand() % MAX_NUMBER_OF_CARDS;

				Card Temp = m_Deck[i];
				m_Deck[i] = m_Deck[RandomNumber];
				m_Deck[RandomNumber] = Temp;
			}

			iCount++;
		}
	}
	else
	{
		cout << "The number of times to shuffle is not a valid value!" << endl;
	}
}
Example #4
0
int main()
{
    unsigned int array[SIZE];
    unsigned int c;

    srand(time(nullptr));	// use current time in seconds as random seed

    for(c = 0 ; c < SIZE ; c++)	// randomize contents of array
        array[c] = rand() * rand();
    cout << "An array of " << SIZE << " random numbers:\n\n";
    for(c = 0 ; c < SIZE ; c++)	// display array (5 numbers per row)
        cout << setw(15) << array[c] << ((c+1)%5 ? "" : "\n");
    cout << endl;

    bucketsort(array, SIZE);	// sort array

    cout << "The same array sorted:\n\n";
    //for( c = 0 ; c < 5 ; c++ )	// print a header?
    //    cout << "     9876543210";
    //cout << '|' << endl;
    for(c = 0 ; c < SIZE ; c++)	// display array (5 numbers per row)
        cout << setw(15) << array[c] << ((c+1)%5 ? "" : "\n");
    cout << endl;

    system("PAUSE");
    return 0;
} // end function main
Example #5
0
// Display message for wrong answer
void displayAfterWrongAnswer()
{
	srand( time(NULL) ); 				// seed random generator
	int numberMessage;					// random number to display different messages
	numberMessage = (rand() % 4 + 1); 	// random number from 1 to 4
	
	// Display different messages based on random number generated
	switch( numberMessage )
	{
		case 1:
			cout << "No. Please try again." << endl;
			break;
		case 2:
			cout << "Wrong. Try once more." << endl;
			break;
		case 3: 
			cout << "Don't give up!" << endl;
			break;
		case 4: 
			cout << "Keep trying." << endl;
			break;
		default:
			cout << "Uknown case..." << endl;
			break;
	}
}
Example #6
0
// Display message for correct answer
// To reduce student fatigue, display four different messages based on generated 
// random number
void displayAfterCorrectAnswer()
{
	srand( time(NULL) );
	int numberMessage;
	numberMessage = (rand() % 4 + 1); // random generate number from 1 to 4

	// Display different messages based on random number generated
	switch( numberMessage )
	{
		case 1:
			cout << "Very good!" << endl;
			break;
		case 2:
			cout << "Excellent!" << endl;
			break;
		case 3: 
			cout << "Nice work!" << endl;
			break;
		case 4: 
			cout << "Keep up the good work!" << endl;
			break;
		default:
			cout << "Uknown case..." << endl;
			break;
	}
}
Example #7
0
int main()
{
   srand( time( 0 ) ); // seed random number generator
   multiplication(); // begin multiplication practice

   system("pause");  // enter any character to exit
   return 0; // indicate successful termination
} // end main
Example #8
0
// constructor fill vector with random integers
MergeSort::MergeSort( int vectorSize )
{
   size = ( vectorSize > 0 ? vectorSize : 10 ); // validate vectorSize
   srand( time( 0 ) ); // seed random number generator using current time

   // fill vector with random ints in range 10-99
   for ( int i = 0; i < size; i++ )
      data.push_back( 10 + rand() % 90 );
} // end MergeSort constructor
int nrand(const int n) {
  if (n <= 0 || n > RAND_MAX) {
    throw domain_error("Argument to nrand is out of range.");
  }

  time_t t = time(nullptr);
  srand(t);
  const int r = rand() % n;
  cout << "r = " << r << endl;
  return r;
}
Example #10
0
void generate_data(double array[], size_t array_len,
                   int upper_bound)
{
    size_t i;
    // Set seed to 1
    srand(1);
    // Generate random numbers between 0 and upper_bound
    for(i = 0; i < array_len; i++)
    {
        array[i] = rand() % upper_bound;
    }
}
void ScenePointSprite::initScene()
{
    compileAndLinkShader();

    glClearColor(0.5f,0.5f,0.5f,1.0f);
    glEnable(GL_DEPTH_TEST);

    //float c = 2.5f;
    //projection = glm::ortho(-0.4f * c, 0.4f * c, -0.3f *c, 0.3f*c, 0.1f, 100.0f);

    angle = (float)(PI / 2.0);

    numSprites = 50;
    locations = new float[numSprites * 3];
    srand( (unsigned int)time(0) );
    for( int i = 0; i < numSprites; i++ ) {
        vec3 p(((float)rand() / RAND_MAX * 2.0f) - 1.0f,
            ((float)rand() / RAND_MAX * 2.0f) - 1.0f,
            ((float)rand() / RAND_MAX * 2.0f) - 1.0f);
        locations[i*3] = p.x;
        locations[i*3+1] = p.y;
        locations[i*3+2] = p.z;
    }

    // Set up the buffers
    GLuint handle;
    glGenBuffers(1, &handle);

    glBindBuffer(GL_ARRAY_BUFFER, handle);
    glBufferData(GL_ARRAY_BUFFER, numSprites * 3 * sizeof(float), locations, GL_STATIC_DRAW);

    delete [] locations;

    // Set up the vertex array object
    glGenVertexArrays( 1, &sprites );
    glBindVertexArray(sprites);

    glBindBuffer(GL_ARRAY_BUFFER, handle);
    glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, ((GLubyte *)NULL + (0)) );
    glEnableVertexAttribArray(0);  // Vertex position

    glBindVertexArray(0);

    // Load texture file
    glActiveTexture(GL_TEXTURE0);
    GLuint tid = SOIL_load_OGL_texture("flower.png", SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_MIPMAPS|SOIL_FLAG_INVERT_Y);
    glBindTexture(GL_TEXTURE_2D, tid);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    prog.setUniform("SpriteTex", 0);
    prog.setUniform("Size2", 0.15f);
}
Example #12
0
//起始乱序分牌
void GameServer::mixedOrder(int x[], int n)
{
    srand(static_cast<unsigned int>(time(0)));
    int index1,index2,tmp;
    for(int i=0;i<n;i++){
        index1=rand()%54;
        index2=rand()%54;
        tmp=x[index1];
        x[index1]=x[index2];
        x[index2]=tmp;
    }
}
Example #13
0
void App::setup()
{
    using std::srand;
    using std::time;

    srand(time(nullptr));
    hideCursor();
    setFpsSampleInterval(1.f);

    m_Timer.start();
    BuildGame(*this);
}
Example #14
0
void Deck::Shuffle()
{
	srand(static_cast<int>(time(NULL)));
	Card * temp;
	int random = 0;
	for (int ii = 0; ii < m_numCards; ii++)
	{
		random = rand() % m_numCards;			// Get a random number within the card range
		temp = m_fullDeck[ii];					// Swap the pointers between the current card
		m_fullDeck[ii] = m_fullDeck[random];	// and the random card.
		m_fullDeck[random] = temp;
	}
}
Example #15
0
File: main.cpp Project: CCJY/coliru
int main() 
{
    srand ( std::time ( 0 ) ); // use current time as seed for random generator
    int random_variable = rand ();
    
    
    cout << "Random value between 0 - 99 :";
    cout << random_variable % ( 99 - 0 + 1 ) + 0 << std::endl;
    
    cout << "Random value between 15 - 99 :";
    cout << random_variable % ( 99 -15 + 1 ) + 15 << std::endl;
   
}
Example #16
0
int main()
{
	double x;
	cout << fixed << setprecision( 0 );
	srand ( time ( 0 ) );
	for ( int count = 1; count <= 100; count++ )
	{
		x = -3 + rand() % 15;
		cout << x << endl;
	}

	return 0;
}
Example #17
0
/*=============================================================================
	Purpose: To shuffle the cards in the Deck into some random order.
=============================================================================*/
void Deck::Shuffle()
{
	srand(static_cast<int>(time(0)));

	for(int i = 0; i < MAX_NUMBER_OF_CARDS; i++)
	{
		int RandomNumber = rand() % MAX_NUMBER_OF_CARDS;

		Card Temp = m_Deck[i];
		m_Deck[i] = m_Deck[RandomNumber];
		m_Deck[RandomNumber] = Temp;
	}
	m_Current_Card = 0;
}
Example #18
0
/*
	Purpose: To shuffle the cards in the Deck once

	Entry: N/A

	Exit: The cards in the deck have now been placed in a random order
*/
void Deck::Shuffle()
{
	srand(static_cast<int>(time(0)));

	cout << "Will shuffle one time." << endl;
	for(int i = 0; i < MAX_NUMBER_OF_CARDS; i++)
	{
		int RandomNumber = rand() % MAX_NUMBER_OF_CARDS;

		Card Temp = m_Deck[i];
		m_Deck[i] = m_Deck[RandomNumber];
		m_Deck[RandomNumber] = Temp;
	}
}
int main()
{
	string verbs[20] = {"eats","runs","climbs", "thinks", "pukes"};
	string nouns[20] = {"tree","taxi","crocodile","chair","asteroid","fish","tangerine"};
	string adjectives[20] = {"fast", "slow", "hairy", "blue", "sad", "difficult"};

	srand(time(NULL));

	BR
	cout << generatePhrase(adjectives, verbs, nouns);
	BR


	return 0;
}
Example #20
0
int *line_segments_generation()
{
	int *lines = new int[LINE_NUM*4];

	srand(time(0));

	for (int i = 0; i < LINE_NUM; i++) {
		lines[4*i] = rand() % W;
		lines[4*i + 1] = rand() % H;
		lines[4*i + 2] = rand() % W;
		lines[4*i + 3] = rand() % H;
	}

	return lines;
}
Example #21
0
int main()
{
    srand( time(NULL) ); // seed random generator

    int maxCount     = 10; 	// maximal number of answer counts
    int count        = 0;	// count anwsers
    int countCorrect = 0;	// count correct answers
    int countWrong   = 0;	// count wrong anwsers

    while( count < maxCount )
    {
        int number1 = generateNumber(); 	// generate one number
        int number2 = generateNumber(); 	// generate second number

        int correctResult = -1;
        correctResult = number1 * number2;	// correct multiplication result

        // display which numbers are being multiplyed
        // To exit loop, input 0
        displayMessage( number1, number2 );
        int userResult = -1; 				// user result of multiplication
        cin >> userResult;					// read input

        // repeat until result is correct
        while( userResult != correctResult )
        {
            ++countWrong; 	// count wrong anwsers
            ++count;		// count anwsers
            displayAfterWrongAnswer();
            cin >> userResult;
        }

        // display message for correct result
        if( userResult == correctResult ) {
            ++count;
            ++countCorrect;
            displayAfterCorrectAnswer();
        }
    }

    if( count != maxCount )
        cout << "Not correct counting!" << endl;

    // display statistics
    statistics( count, countCorrect );

    return 0;
}// end main function
Example #22
0
int main ()
{
	srand ( time ( 0 ) );
	int a;
	string c;
	int levelup = 1;
	int rCount = 0;
	int level;
	cout << "Enter your level ( 1 for one-bit, 2 for two-bit and so on ): ";
	cin >> level;
	for ( int count = 1; count <= level; count++ )
		levelup *= 10;
		levelup--;
	for ( int count = 1; count <= 10; count++ )
	{
		int x = 1 + rand() % 9, y = 1 + rand() % levelup;
		cout << "What is " << x << " multiple by " << y << "?" << endl;
		int z;
		cin >> z;
		a = 1 + rand() % 4;
		switch ( a )
		{
			case 1:
				c = "Very well";
				break;
			case 2:
				c = "Perfectly!"; 
				break;
			case 3:
				c = "Great job!";
				break;
			case 4:
				c = "Keep working in the same spirit!";
				break;
		}
		if ( z == x * y )
		{
			cout << c << endl;
			rCount++;
		}
		else
			repeat( x, y, z );
	}
	
	if ( ( rCount * 10 ) < 75 )
		cout << "Please ask your teacher to help you!" << endl;
	return 0;
}
Example #23
0
// this method creates new numbers and puts them in the vector numbers
void create() {
    // new random seed
    srand(time(NULL));
    while (running) {
        // creating a random number between 1 and 1000
        int random = rand() % 1000 + 1;
        // locking the mutex
        m.lock();
        cout << "Creator: Random number " << random << endl;
        // putting the number
        numbers.push_back(random);
        m.unlock();
        // sleeps for 300 milliseconds before creating a new number
        sleep_for(milliseconds(300));
    }
}
Example #24
0
void ScenePointSprite::initScene()
{
    compileAndLinkShader();

    glClearColor(0.5f,0.5f,0.5f,1.0f);

    glEnable(GL_DEPTH_TEST);

    numSprites = 50;
    locations = new float[numSprites * 3];
    srand( (unsigned int)time(0) );
    for( int i = 0; i < numSprites; i++ ) {
        vec3 p(((float)rand() / RAND_MAX * 2.0f) - 1.0f,
               ((float)rand() / RAND_MAX * 2.0f) - 1.0f,
               ((float)rand() / RAND_MAX * 2.0f) - 1.0f);
        locations[i*3] = p.x;
        locations[i*3+1] = p.y;
        locations[i*3+2] = p.z;
    }

    // Set up the buffers
    GLuint handle;
    glGenBuffers(1, &handle);

    glBindBuffer(GL_ARRAY_BUFFER, handle);
    glBufferData(GL_ARRAY_BUFFER, numSprites * 3 * sizeof(float), locations, GL_STATIC_DRAW);

    delete [] locations;

    // Set up the vertex array object
    glGenVertexArrays( 1, &sprites );
    glBindVertexArray(sprites);

    glBindBuffer(GL_ARRAY_BUFFER, handle);
    glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, ((GLubyte *)NULL + (0)) );
    glEnableVertexAttribArray(0);  // Vertex position

    glBindVertexArray(0);

    // Load texture file
    GLuint w, h;
    const char * texName = "../media/texture/flower.bmp";
    BMPReader::loadTex(texName, w, h);

    prog.setUniform("SpriteTex", 0);
    prog.setUniform("Size2", 0.15f);
}
Example #25
0
int main(void) {
	unsigned seed;

	//cout << "Enter seed: ";
	//cin >> seed;
	seed = time(0);
	srand(seed);

	for (int counter = 1; counter <= 20; counter++) {
		cout << setw(10) << (1 + rand() % 6);

		if (counter % 5 == 0)
			cout << endl;
	}

	return 0;
}
Example #26
0
int main()
{
    enum Status {CONTINUE, WON, LOST};

    int myPoint;
    Status gameStatus;

    srand(time(0));

    int sumOfDice = rollDice();

    switch (sumOfDice)
    {
        case 7:
        case 11:
            gameStatus = WON;
            break;
        case 2:
        case 3:
        case 12:
            gameStatus = LOST;
            break;
        default:
            gameStatus = CONTINUE;
            myPoint = sumOfDice;
            cout << "Point is " << myPoint << endl;
            break;
    }

    while (gameStatus == CONTINUE)
    {
        sumOfDice = rollDice();

        if (sumOfDice == myPoint)
            gameStatus = WON;
        else if(sumOfDice == 7)
            gameStatus = LOST;
    }

    if (gameStatus == WON)
        cout << "Player wins" << endl;
    else
        cout << "Player loses" << endl;

    return 0;
}
Example #27
0
//------------------------------------------------------------------------------
int main(void)
{
	my1TestBot cTestBot;
	int count = 0;
	srand(time(0x0));
	// main drive loop
	while(1)
	{
		cTestBot.CaptureView();
		cTestBot.ColorDirect();
		cTestBot.CaptureSens();
		cTestBot.Evaluate();
		if(cTestBot.ZeroDrive()) break;
		cTestBot.MoveDrive();
		count++;
		if(count==MAX_COUNT) break;
	}
	return 0;
}
int main(int argc, char *argv[])
{
	srand(time(NULL));
	FILE *fd;
	static size_t rand_num;
	static long rand_min = 0L;
	static long rand_max = 1000000L;

	fd = fopen("unsorted.txt", "w");
	
	switch(argc) {
	case 2:
		rand_num = atol(argv[1]);
		break;
	case 3:
		rand_num = atol(argv[1]);
		rand_max = atol(argv[2]);
		break;
	case 4:
		rand_num = atol(argv[1]);
		rand_min = atol(argv[2]);
		rand_max = atol(argv[3]);
		break;
	default:
		printf("Usage: %s rand_num\n", argv[0]);
		printf("Usage: %s rand_num rand_max\n", argv[0]);
		printf("Usage: %s rand_num rand_min rand_max\n", argv[0]);
		break;
	}//switch

	for(int i = 1; i <= rand_num; ++i) {
		if(i % 10 == 0)
			fputc('\n', fd);
		fprintf(fd,
				"%ld ",
				(long)(rand() * (rand_max - rand_min)/RAND_MAX
					+rand_min)); 
	}//for
	
	fclose(fd);
	
	return 0;
}//main
Example #29
0
//-----------------------------------------------------------------------------
void PrUnorganized_OP::scanRawData(std::istream& is, 
				   int num_cells,
				   double noise)
//-----------------------------------------------------------------------------
{
  int numpnts;
  is >> numpnts;
  is >> nInt_;
  uv_.resize(numpnts);
  double *points = new double[3*numpnts];
  int i;
  double x,y,z;
  unsigned int seed = 1;
  srand(seed);
  double random;
  //Alter interior points by up to noise in either direction
  // thus xnew will be between x-noise and x+noise.

  for(i=0; i<nInt_; i++)
  {
    is >> x >> y >> z;
    random = static_cast<double>(rand())/RAND_MAX; // random is a number between 0 and 1
    points[3*i] = x + (2.0 * random - 1.0) * noise;
    random = static_cast<double>(rand())/RAND_MAX; // random is a number between 0 and 1
    points[3*i+1] = y + (2.0 * random - 1.0) * noise;
    random = static_cast<double>(rand())/RAND_MAX; // random is a number between 0 and 1
    points[3*i+2] = z + (2.0 * random - 1.0) * noise;
    uv_[i].x() = 0.0;
    uv_[i].y() = 0.0;
  }
  for(i=nInt_; i<numpnts; i++)
  {
    is >> x >> y >> z;
    points[3*i] = x;
    points[3*i+1] = y;
    points[3*i+2] = z;
    uv_[i].x() = 0.0;
    uv_[i].y() = 0.0;
  }
  cellstruct_.setNumCells(num_cells);
  cellstruct_.attach(numpnts,points);
  delete points;
}
Example #30
0
int main()
{
   const int arraySize = 7; // ignore element zero
   int frequency[ arraySize ] = {}; // initialize elements to 0

   srand( time( 0 ) ); // seed random number generator

   // roll die 6,000,000 times; use die value as frequency index
   for ( int roll = 1; roll <= 6000000; roll++ )       
      frequency[ 1 + rand() % 6 ]++;

   cout << "Face" << setw( 13 ) << "Frequency" << endl;

   // output each array element's value
   for ( int face = 1; face < arraySize; face++ )  
      cout << setw( 4 ) << face << setw( 13 ) << frequency[ face ] 
         << endl;

   return 0; // indicates successful termination
} // end main