Simulation algorithm: The whole process of simulation, by changing the parameters of the mathematical model, further observing the state changes these parameters change due process.
Algorithmic thinking: Using stochastic functions to simulate unpredictable occurrences in nature. (Srand () and rand () function generate a random number)
The simulation algorithm means that the whole process is completed and gone through. How to describe the topic, the procedure is how to carry out.
Example one: guess the digital computer randomly generates a 1-100 integer, the user pushes, each time the test gives a different hint. Code:
#include <iostream> #include <stdlib.h> #include <time.h>using namespace Std;int main () { Srand ( Time (NULL)); int count=0; int num = rand ()%100 + 1; int guess; cout << "Please enter the value of the push:"; Cin >> guess; do{ if (guess > num) { count++; cout << "You guessed the value is big \ n"; cout << "Please enter the value of the push again:"; Cin >> guess; } else if (guess < num) { count++; cout << "You guessed the value is small \ n"; cout << "Please enter the value of the push again:"; Cin >> guess; } } while (guess! = num); count++; cout << "I guessed it!\ n "; cout << "Total guessed" << count << "times \ n"; return 0;}
Execution Result:
Example two: Simulated dice game by the user to enter the number of participants and dice. The computer then randomly generates points for each dice and then counts each person's points.
Code:
#include <iostream> #include <stdlib.h> #include <time.h>using namespace std;void play (int n); void Play (int n) { int i,m=0,t=0; for (i=0; i<n; i++) { t=rand ()%6+1; m+=t; cout << "<< (i+1) <<" Dice in points: "<< t <<" \ n "; } cout << "Total" << m << "point \ n";} int main () { int people,numbers; do{ Srand (Time (NULL)); cout << "Please enter the number of participants:"; Cin >> People; if (people = = 0) break; cout << "Please enter the number of dice:"; CIN >> numbers; if (numbers = = 0) break; for (int i=0; i<people; i++) { cout << "<< (i+1) <<" Players roll the dice case: \ n "; Play (numbers); } } while (1); return 0;}
Execution Result:
Note:
How to use C + + to generate random numbers
1) provide a seed to Srand (), which is a unsigned int type.
2) call Rand (), which returns a random number (between 0 and Rand_max) based on the seed value provided to Srand ();
3) Depending on the need to call rand () multiple times. Thus, a new random number is obtained continuously;
4) No matter what time. are able to provide srand () with a new seed, further "randomize" the output of rand ().
A general expression formula for generating a certain range of random numbers
To obtain a random integer [A, b], use (rand ()% (b-a)) + A;
To obtain a random integer [A, b], use (rand ()% (b-a+1)) + A;
To obtain a random integer (A, b], use (rand ()% (b-a)) + A + 1;
General formula: A + rand ()% n. Where a starts value, and the N range is an integer.
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Simulation algorithm data structure and algorithm C + + implementation