srand函数简介

srand函数是随机数发生器的初始化函数。

原型:void srand(unsigned seed);

用法:它需要提供一个种子,这个种子会对应一个随机数,如果使用相同的种子后面的rand()函数会出现一样的随机数。如:srand(1); 直接使用1来初始化种子。不过为了防止随机数每次重复,常常使用系统时间来初始化,即使用time函数来获得系统时间,它的返回值为从 00:00:00 GMT, January 1, 1970到现在所持续的秒数,然后将time_t型数据转化为(unsigned)型再传给srand函数,即: srand((unsigned)time(&t)); 还有一个经常用法,不需要定义time_t型t变量,即: srand((unsigned)time(NULL)); 直接传入一个空指针,因为你的程序中往往并不需要经过参数获得的t数据。

例子:#include

#include

#include

#defineMAX 10

 

int main(void)

{

 intnumber[MAX] = {0};

 inti;

 srand((unsigned)time(NULL));

 for(i= 0; i < MAX; i++)

 {

 number[i]= rand() % 100;

 printf("%d", number[i]);

 }

 printf("\n");

 return0;

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/462088.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python库--pandas--Series.str--字符串处理

目录 原数据字符大小写转换字符串拼接字符填充/插入/扩展字符串内容判断查找统计转码删减/截取分割/替换原数据 import pandas as pd a pd.Series([aSd, asd, dfd fsAsf sfs]) b pd.Series([None, asd, fgh]) indexab0aSdNone1asdasd2dfd fsAsf sfsfgh字符大小写转换 a.str.l…

linux下多线程之pthread_detach(pthread_self())

写个碰到的问题&#xff0c;记录下自己的技术之路点滴 pthread_detach(pthread_self()) linux线程执行和windows不同&#xff0c;pthread有两种状态joinable状态和unjoinable状态&#xff0c; 如果线程是joinable状态&#xff0c;当线程函数自己返回退出时或pthread_exit时都不…

Unique Binary Search Trees

使用带标记的DP int result[1000];int numTrees(int n) {result[0] 1;result[1] 1;result[2] 2;// Start typing your C/C solution below// DO NOT write int main() functionif(result[n] ! 0)return result[n];int num 0, t;for(t 1; t < n; t){num num numTrees(…

ERROR: Start Page at 'www/index.html' was not found

用Xcode 4.3.2新建了一个PhoneGap的应用&#xff0c;www目录下存在index.html文件&#xff0c;但是运行的时候&#xff0c;报错&#xff1a;ERROR: Start Page at www/index.html was not found&#xff0c;这是PhoneGap和Xcode 4还不兼容导致的。 可以右键项目名->Add File…

Linux线程-互斥锁pthread_mutex_t

在线程实际运行过程中&#xff0c;我们经常需要多个线程保持同步。这时可以用互斥锁来完成任务&#xff1b;互斥锁的使用过程中&#xff0c;主要有pthread_mutex_init&#xff0c;pthread_mutex_destory&#xff0c;pthread_mutex_lock&#xff0c;pthread_mutex_unlock这几个函…

百度前端技术学院,学习第一天。

有基础&#xff0c;所以第一天的了解基本可以跳过。 略.. 转载于:https://www.cnblogs.com/devanwu/p/11149051.html

ULS 日志为空

解决方案&#xff1a; 1.保存为ChangeAccounts_SPTraceV4.ps1&#xff0c;运行 # Get the tracing service. $farm Get-SPFarm $tracingService $farm.Services | where {$_.Name -eq "SPTraceV4"} # Get the "svc_sp_services" managed account. $manag…

2013 ACM/ICPC Asia Regional Changsha Online - C

2019独角兽企业重金招聘Python工程师标准>>> 竟然没写出来 还是比较坑&#xff0c;好吧 Color Representation Conversion Time Limit: 1 Second Memory Limit: 32768 KB So far, there are many color models in different area. For screen display, the most …

vs2017常量文本字符串无法转换成char*

vs2017中这种写法编译不通过&#xff1a;char* pTest "hello"; 解决方法&#xff1a;在C/C选项-命令行-附加选项中增加&#xff1a;/Zc:strictStrings- (Disable string literal type conversion)转载于:https://www.cnblogs.com/dqloveu/p/11149222.html

linux中DIR、dirent、opendir()、readdir()、closedir()函数的使用

一、 1、DIR 属性&#xff1a;数据类型&#xff1b; 头文件&#xff1a;#include <dirent.h> 用法&#xff1a;定义一个指向文件目录的指针&#xff1b; 举例&#xff1a;DIR *dirpt null; 2、dirent 属性&#xff1a;数据类型&#xff0c;结构体&#xff1b; 头…

BNUOJ 4358 左手定则 搜索

题目链接&#xff1a;http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid4358 一道很好的搜索题&#xff0c;要注意DFS函数的写法&#xff0c;特别是return的写法。 View Code 1 #include <iostream>2 #include <cstring>3 #include <cstdio>4 using names…

CentOS安装Confluence Wiki步骤

参考&#xff1a;http://supernetwork.blog.51cto.com/2304163/1187066参考&#xff1a;http://yjiang.tk/?p1085需要的文件CentOS-6.5-x86_64-minimal.isojre-7u67-linux-x64.rpmatlassian-confluence-5.4.4-x64.binmysql-connector-java-5.1.32-bin.jarconfluence5.1-crack.…

memset()、memcpy()、memcmp()的使用方法

1、void *memset(void *s,int ch,size_t n); 属性&#xff1a;函数&#xff1b; 用法&#xff1a;将s中当前位置后面n个字节用ch替换&#xff0c;并返回s&#xff1b; 头文件(C语言)&#xff1a;#include <string.h> 举例&#xff1a; char str_addr[50]; memset(str…

provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接错误的解决方法...

这个错误主要有以下几个原因造成&#xff1a; 1. 错误的连接字符串&#xff1a;例如数据源的实例名称“\"错误写成"/"了 2、Named Pipes(NP)没有启动 其他原因&#xff0c;详见&#xff1a;http://social.msdn.microsoft.com/forums/en-US/sqlreportingservices…

使用VS2015编写C/C++开始步骤

下面围绕如何建立工程、如何添加代码和运行展开说明。 一、建立工程 &#xff08;1&#xff09;打开VS2015&#xff0c;然后在菜单栏中选择file—>New—>Project&#xff1b; &#xff08;2&#xff09;在弹出的界面中&#xff0c;选择Win32&#xff0c;编辑工程名字、…

ruby中正则表达式最小匹配与最大匹配

正则表达式中&#xff0c;默认的是最大匹配&#xff0c;即贪婪模式&#xff0c;但有些时候&#xff0c;要最小匹配&#xff0c; 请看下面的例子&#xff1a;(ruby) str "abbbbbdwwdwwwede"puts str[/a.*(dw)/] 结果为&#xff1a;abbbbbdwwdw str "abbbbbdwwd…

sprintf()、fprintf()的使用方法

1、int sprintf(char *str,char *format,[forgument,..]); 属性&#xff1a;函数&#xff1b; 用法&#xff1a;将格式化的字符写入字符串&#xff1b; 头文件&#xff1a;#include <stdio.h> 参数&#xff1a;char *str:要写入字符串的指针&#xff1b; char *form…

界面小项目之小米登录注册

<!DOCTYPE html><html><head> <meta charset"UTF-8"> <title>登录注册</title> <style> body, ul { margin: 0; padding: 0; list-style: none; } .logi…

Crusher Django 学习笔记4 使用Model

http://crusher-milling.blogspot.com/2013/09/crusher-django-tutorial4-using-basic.html 顺便学习一下FQ Crusher Django转载于:https://www.cnblogs.com/impact-crusher/p/3339029.html

C库中的输入函数、输出函数

关于这些C库输入函数、输出函数的用法&#xff0c;可以在命令行输入“man 3 xxx”来查询。 一、printf()函数、scanf()函数 1、printf()函数 函数模型 int printf(const char *format, ...); 函数作用 把存储在计算机中的二进制格式的数值&#xff0c;按照转换说明&#xff0c…