随机函数

LJ posted @ Jun 09, 2010 12:01:49 AM in 程序设计随笔 with tags 程序设计 , 1613 阅读

 随机函数的实现

        在编程过程中经常要用到随机数,通过调用库函数rand()来产生随机数。在调用rand()函数前一般要先调用 srand (unsigned int seed)产生随机种子。seed的选取一般可以采用系统时间来设置。产生随机数的范围在0~RAND_MAX。RAND_MAX定义在stdlib.h,其值为32767。

        假如我们要产生10个0~10之间的随机数,可以通过以下方式

 

#include<stdlib.h>
#include<stdio.h>
#include<time.h>

main()
{
    int i,j;
    srand((int)time(0));
    for(i=0;i<10;i++)
    {
        j=rand()%10;
        printf(" %d ",j);
    }
}

 

        关于srand(unsigned int seed)函数和rand()函数是库函数提供给我们的接口,也可以定义自己的函数来模拟这两个函数的实现

 

#include <time.h>
#include <stdlib.h>
#include <stdio.h>

unsigned long int next = 1;

void LJ_srand(unsigned int seed)
{
	next = seed;
}

int LJ_rand()
{
	next = next*1103515+12345;
	return (unsigned int)(next/65536)%32767;
}

main ()
{
	int i;
LJ_srand( time(NULL) );   
 for( i = 0; i < 10; i++ )      
	 printf( "Random number #%d: %d\n", i,
                 LJ_rand()%10);
}

 

rand()函数和srand()函数之间的通信是通过一个全局变量next来交互的,不调用srand()函数时,默认的next的值为1,那么每次产生的随机数是一样的。

参考c programing language

 

 

 

 

JDC Result Dinajpur 说:
Sep 03, 2022 12:34:21 AM

Dinajpur also one of the best education in the country and the Dinajpur Division also successfully completed the Grade 8 terminal examination tests along with other education boards or divisions of the country, and there are a huge number of students are participating from Dinajpur Board also, JDC Result Dinajpur and all are waiting to check their JSC Result 2022 with full or total marksheet. Both of Junior School Certificate & Junior Dakhil Certificate students are waiting to get official result date to check their total GPA Grade point with subject wise marksheet, the Dinajpur Division also completed those STD-8 final exams in the month of November as per date sheet issued by Bangladesh Secondary and Higher Secondary Education Board and the result is also announced as per the schedule.

AP SSC fa 4 Model Pa 说:
Sep 09, 2022 01:48:26 AM

Formative Assessment means not only an examination, it includes various aspects such as Examination in completed lessons, Reflections, Project work done on the allotted topic and Self also Prepared notes etc. AP SSC fa 4 Model Paper Candidates of Telugu Medium, English Medium & Urdu Medium of the AP State can download the AP 10th Class FA 4 Model Paper 2023 Pdf with answers for the regular exams conducted by the Directorate of Government Examinations, Andhra Pradesh.

civaget 说:
Jan 18, 2024 01:28:51 AM

펀초이스 empowers you to make smart choices for your well-being, ensuring your massage experience is unmatched.

pavzi.com 说:
Jan 22, 2024 02:16:53 PM

Pavzi.com provides all the news about Gadgets, the Economy, Technology, Business, Finance and many more. The main concept or our aim behind this website has been the will to provide resources with full information on each topic which can be accessed through the Internet. To ensure that every reader gets what is important and worthy about the topic they search and link to hear from us.pavzi.com Our site is a multiple Niche or category website which will ensure to provide information and resources on each and every topic. Some of the evergreen topics you will see on our website are Career, Job Recruitment, Educational, Technology, Reviews and others.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter