C/C++生成随机数
2013-10-25 14:07
267 查看
原文转载自:http://www.oschina.net/code/snippet_54100_5029
1.[代码][C/C++]代码
01 | #include <stdio.h> |
02 | #include <stdlib.h> |
03 |
04 | int main( int argc, char *argv[]) |
05 | { |
06 | int i; |
07 | for (i=0; |
08 | printf ( "%d\n" , rand ()); |
09 | } |
10 | return 0; |
11 | } |
2.[代码][C/C++]代码
01 | /* |
02 | 上述方法产生随机数每次不同,但每次运行时产生的顺序是相同的。原因是每次的种子都相同,所以会产生相同的随机数列。一般使用时间作为种子保证每次的种子都不同。简单的实现代码: |
03 | */ |
04 | #include <stdio.h> |
05 | #include <stdlib.h> |
06 | #include <time.h> |
07 |
08 | int main( int argc, char *argv[]) |
09 | { |
10 | int i; |
11 | srand (( int ) time (0)); |
12 | for (i=0; |
13 | printf ( "%d\n" , rand ()); |
14 | } |
15 |
16 | return 0; |
17 | } |
3.[代码][C/C++]代码
01 | /* |
02 | 如果要产生其他范围内的整数,可以使用取余运算实现。以下代码为产生0~100之间的随机数: |
03 | */ |
04 | #include <stdio.h> |
05 | #include <stdlib.h> |
06 | #include <time.h> |
07 |
08 | int main( int argc, char *argv[]) |
09 | { |
10 | int i; |
11 | srand (( int ) time (0)); |
12 | for (i=0; |
13 | printf ( "%d\n" , rand ()%100); |
14 | } |
15 |
16 | return 0; |
17 | } |
相关文章推荐
- C++生成随机数:正态分布(normal distribution)
- 教你如何用C++快速生成1000万个随机数
- C++随机数生成方法
- C++生成随机数:二项分布(binomial distribution)
- C/C++随机数生成 rand() srand()
- C++中生成随机数的几点注意事项
- C++生成随机数——生成任意范围内的等概率随机数
- C++ 随机数生成
- C++ 随机数生成
- c++随机数生成
- 随机数生成c++(转)
- C++生成随机数:负二项分布/帕斯卡分布(negative binomial distribution)
- c++生成随机数
- C++中随机数的生成
- c++如何生成随机数
- C/C++ 中生成特定范围内的随机数
- c++实现输入n生成n bit的随机数
- C++随机数--——生成任意范围内等概率随机数“足够好”的做法
- c++生成随机数
- C++中rand()生成随机数