当前位置:网站首页>The simulation generates random numbers and calculates the birthday of each student
The simulation generates random numbers and calculates the birthday of each student
2022-04-22 04:42:00 【tonyaqiqi】
When generating random numbers , I chose time(0), But it can always generate the same random number . I began to attribute it to computers running too fast , A certain interval is required , So this random number can't be used . After consulting the boss , The reason is that srand() and rand All in a loop . need srand Out of the loop ,rand Use together in the cycle , In this way, a real random number can be generated .
#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
const float Day = 365.0;
int getrand();
int main()
{
int birth[50] = {
0 },i,j,l,k,flag,count=0;
float p;
srand((unsigned)time(0));
for (j = 0; j < 10000; j++)
{
flag = 0;
for (i = 0; i <50; i++)
{
birth[i] = rand()%365+1;
}
for (k = 0; flag == 0 && k <49; k++)
{
for (l = k + 1; l < 50; l++)
{
if (birth[k] == birth[l])
{
flag = 1;
break;
}
}
}
if (flag == 1)
count++;
}
cout << count<<endl;
p = (float)count / j;
p = 1 - p;
cout << p;
return 0;
}
``
版权声明
本文为[tonyaqiqi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220437167390.html
边栏推荐
- 2022 P cylinder filling training questions and answers
- jsp hello world中文乱码
- Tensorfloweager's view is turned off and on
- 解决IDEA中文乱码问题(配置文件乱码)
- 创意几何点线面三角形孟菲斯海报banneer广告设计ai矢量背景素材
- Deployment of web server, personal experience
- 链表(Linked List)第一篇
- Unity simple UI prefix tree red dot system
- Leetcode sword finger offer 15 Number of 1 in binary
- 2022t elevator repair test exercises and online simulation test
猜你喜欢
![[experience] Why does the IP address of HP printer start with 169.254](/img/e7/5068a565b57f066377fcb5754bc4bd.png)
[experience] Why does the IP address of HP printer start with 169.254

LeetCode 剑指 Offer 43. 1~n 整数中 1 出现的次数***

Paper reading (48): a library of optimization algorithms for organizational design

2022 P cylinder filling training questions and answers

Leetcode refers to offer 43 Number of occurrences of 1 in integers 1 ~ n***

一文告诉你分析即服务(AaaS)到底是什么

安装opencv时遇到的报错

crypto-js加密算法库【安装教程、缓存加密】

【S32K3_STM&PIT_MCAL】

Solve the problem of Chinese garbled code in idea (garbled code in configuration file)
随机推荐
不同品牌设备如何兼容实现互联?一文教你看懂
rpc error: code = Unavailable desc = error reading from server: EOF
2022t elevator repair test questions and online simulation test
AT32 MCU Audio 24bit例程
目标检测--轻量级网络(截至2022-04-21)
Leetcode refers to offer 43 Number of occurrences of 1 in integers 1 ~ n***
Shell variables $, $@, $0, $1, $2, ${},%% use explanation and easy-to-use shell formatting tools
C language learning record -- use and analysis of string function (1)
5_ Data analysis - Data Visualization
Leetcode sword finger offer 22 The penultimate node in the linked list
H7-tool releases firmware v2 15. For offline recording, the full series SPI flash of Renesas, Hetai and is25wp are added (2022-04-14)
2022年山东省安全员C证考试题及在线模拟考试
Financial retail map - transaction flow warning map
DNS domain name system - directory service of the Internet
I'll test the timing again
rpc error: code = Unavailable desc = error reading from server: EOF
链表第三篇
Unity 简单UI前缀树红点系统
AT32 MCU F435/437 DFU DEMO
WinPcap get device list