1、获取普通的随机数 int n = rand(); 2、获取1-n之间的一个随机数 int n = rand() % n + 1; 3、增加随机性 使用srand以时间作为seed,增强随机性 #include <bits/stdc++.h> using namespace std; int main(){ srand(time(0)); int res = rand() % n + 1; return 0; }