Đây là mã để mô phỏng N con lăn xúc xắc. Điều này có thể được thực hiện bằng cách tạo số ngẫu nhiên từ 1 đến 6.
Thuật toán
Begin Declare n Read n For i = 0 to n-1 do Generate sequence with rand() mod 6 + 1 Print the sequence Done End
Mã mẫu
#include <iostream>
using namespace std;
int main(int argc, char **argv) {
cout << "Enter the number of dice: ";
int n;
cin >> n;
cout << "The values on dice are: ";
for (int i = 0; i < n; i++)
cout << (rand() % 6) + 1<<" ";
} Đầu ra
Enter the number of dice: The values on dice are: 2 5 4 2 6 2 5