我想从一组数字中产生一个随机数,例如: 1,2,3,12,11,23,45,54,10,10,12,23,35,24。使用rand和srand,我每次都可以生成不同的随机数,但我想通过从一个组生成随机数来对结果进行条件化。
发布于 2014-11-14 10:43:31
示例代码
拿一个阵列
int a[]={ 1, 2, 3, 12, 11, 23, 45, 54, 10, 10, 12, 23, 35, 24};
int i = rand()%14; // here 14 is number of element in array.
i
将在0
到13
之间接收一个值
最后
printf("%d",a[i]); //print that array index
https://stackoverflow.com/questions/26927978
复制相似问题