นี่คือโค้ดสำหรับจำลองการทอยลูกเต๋า N ลูก สามารถทำได้โดยการสร้างตัวเลขสุ่มระหว่าง 1-6
อัลกอริทึม
เริ่มต้น
ประกาศ n
อ่าน n
For i = 0 ถึง n-1
สร้างลำดับด้วย rand() mod 6 + 1
พิมพ์ลำดับ
สิ้นสุด
โค้ดตัวอย่าง
#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 << " ";
}
ผลลัพธ์ตัวอย่าง
Enter the number of dice: 6 The values on dice are: 2 5 4 2 6 2 5