⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 random.c

📁 书名:C语言科学与艺术,以前交钱下载的
💻 C
字号:
/* * File: random.c * Last modified on Mon Sep 13 10:42:45 1993 by eroberts * -------------- * Implements the random.h interface. */#include <stdio.h>#include <stdlib.h>#include <time.h>#include "genlib.h"#include "random.h"/* * Function: Randomize * ------------------- * This function operates by setting the random number * seed to the current time.  The srand function is * provided by the <stdlib.h> library and requires an * integer argument.  The time function is provided * by <time.h>. */void Randomize(void){    srand((int) time(NULL));}/* * Function: RandomInteger * ----------------------- * This function first obtains a random integer in * the range [0..RAND_MAX] by applying four steps: * (1) Generate a real number between 0 and 1. * (2) Scale it to the appropriate range size. * (3) Truncate the value to an integer. * (4) Translate it to the appropriate starting point. */int RandomInteger(int low, int high){    int k;    double d;    d = (double) rand() / ((double) RAND_MAX + 1);    k = (int) (d * (high - low + 1));    return (low + k);}/* * Function: RandomReal * -------------------- * The implementation of RandomReal is similar to that * of RandomInteger, without the truncation step. */double RandomReal(double low, double high){    double d;    d = (double) rand() / ((double) RAND_MAX + 1);    return (low + d * (high - low));}/* * Function: RandomChance * ---------------------- * This function uses RandomReal to generate a number * between 0 and 100, which it then compares to p. */bool RandomChance(double p){    return (RandomReal(0, 1) < p);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -