C程序生成一定范围内的随机数

Random numbers just numbers that lie within a range and any of the numbers can occur.

随机数只是在一个范围内的数字,任何数字都可能出现。

In programming, we come through a lot of scenarios where we need to generate random numbers. Like for dice games, lottery system, etc. In the c programming language, there is an inbuilt method to take care of this.

在编程中,我们遇到许多需要生成随机数的场景。 就像骰子游戏,彩票系统等一样。在c编程语言中,有内置的方法来处理此问题。

srand() and rand() functions are used to generate random numbers. Now, let's dig deep into these methods and understand their working.

srand()rand()函数用于生成随机数 。 现在,让我们深入研究这些方法并了解它们的工作原理。

rand()函数 (rand() function)

The rand() function in the C programming language is used to generate a random number. The return type is of rand() function is an integer.

C编程语言中的rand()函数用于生成随机数。 返回类型为rand(),函数为整数。

C code to generate a random number

C代码生成一个随机数

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

int main(void)
{
    printf("Random number is:  %d ", rand());
    return 0;
}

Output

输出量

Random number is:  1804289383

srand()函数 (srand() function)

The srand() function is used to set the starting value for the series of random integers. You can use the srand() to set the seed of the rand function to a different starting point.

srand()函数用于设置一系列随机整数的起始值。 您可以使用srand()将rand函数的种子设置为其他起点。

The parameters that are passed into the srand() function are the starting values for the rand method.

传递给srand()函数的参数是rand方法的起始值。

C code to generate random numbers using srand()

C代码使用srand()生成随机数

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

int main(void)
{
    srand(time(0));
    printf("%d ", rand());
    return 0;
}

Output

输出量

1370632410

生成一定范围内的随机数 (Generating random numbers within a range )

The rand() function generates random numbers that can be any integer value. But, to generate random numbers within a specific range, we have a formula that returns a random number between given ranges.

rand()函数生成可以是任何整数值的随机数。 但是,要生成特定范围内的随机数 ,我们有一个公式可以返回给定范围之间的随机数。

Formula:

式:

    number = (rand() % (upper - lower + 1)) + lower

Program to generate random numbers from 1 to 6

程序生成1到6的随机数

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

int main()
{
    int lower = 1, upper = 6, count = 10;

    srand(time(0));

    printf("The random numbers are: ");
    for (int i = 0; i < count; i++) {
        int num = (rand() % (upper - lower + 1)) + lower;
        printf("%d ", num);
    }

    return 0;
}

Output

输出量

The random numbers are: 1 5 4 4 4 2 3 2 4 3

Explanation:

说明:

This program declares a variable num that generates a random number between 1 to 6. for doing this we will initialize the lower limit of the rand to 1 and The upper limit of the rand function to 6. this will generate a random number between 1 to 6 using the formula.

该程序声明一个变量num,该变量生成一个1到6之间的随机数。为此,我们将rand的下限初始化为1并将rand函数的上限初始化为6。这将生成一个1到6之间的随机数。 6使用公式。

Program to generate random number between 1 to 100

程序生成1到100之间的随机数

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

int main()
{
    int lower = 1, upper = 100, count = 10;

    srand(time(0));

    printf("The random numbers are: ");
    for (int i = 0; i < count; i++) {
        int num = (rand() % (upper - lower + 1)) + lower;
        printf("%d ", num);
    }

    return 0;
}

Output

输出量

The random numbers are: 50 22 52 69 24 98 39 31 77 97


翻译自: https://www.includehelp.com/c-programs/generate-random-numbers-within-a-range.aspx

  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用Python语言编写程序生成包含20个随机数的列表。具体代码如下: ```python import random # 生成包含20个随机数的列表 random_list = [random.randint(1, 100) for _ in range(20)] # 输出随机数列表 print(random_list) ``` 运行程序后,会输出一个包含20个随机数的列表,例如: ``` [87, 12, 56, 34, 98, 23, 45, 67, 89, 10, 78, 90, 43, 21, 54, 76, 32, 65, 99, 88] ``` 其中,`random.randint(1, 100)`表示生成1到100之间的随机整数,`range(20)`表示生成20个随机数。 ### 回答2: 编写程序生成包含20个随机数的列表需要使用编程语言和随机数生成函数。在这里以Python语言为例,使用random模块中的randint函数来生成随机数,并使用列表来存储这些随机数。下面是具体实现过程: 1. 导入random模块 ``` python import random ``` 2. 创建一个空列表,用于存储随机数 ``` python numbers = [] ``` 3. 使用for循环生成20个随机数,并将它们添加到列表中 ``` python for i in range(20): numbers.append(random.randint(1, 100)) ``` 4. 打印列表中的随机数 ``` python print(numbers) ``` 完整代码如下: ``` python import random numbers = [] for i in range(20): numbers.append(random.randint(1, 100)) print(numbers) ``` 这个程序将会生成一个包含20个随机数的列表并输出到屏幕上。每次运行程序生成随机数都将不同。 ### 回答3: 编写程序生成包含20个随机数的列表是一项比较简单的编程任务。下面我来讲一下如何实现: 首先,我们需要使用一个随机数生成函数将随机数生成出来。在Python中,random模块提供了生成随机数的函数。其中,最常用的函数是random()函数,这个函数会生成0到1之间的随机浮点数。 接下来,我们需要使用循环来生成20个随机数。可以使用for循环,这样可以很方便地生成指定数量的随机数。 最后,我们需要将生成随机数添加到一个列表中。可以使用append()方法将随机数添加到列表中。 具体的代码如下: ``` import random random_list = [] for i in range(20): random_number = random.random() random_list.append(random_number) print(random_list) ``` 运行结果可能会类似于: ``` [0.9561246322012614, 0.17317415918424045, 0.22669059481112825, 0.16120695994589315, 0.6383601901045725, 0.6921602547235553, 0.1767487462451229, 0.9529959876914038, 0.2905264448211163, 0.8277344831546224, 0.8539449985919145, 0.7113288103652897, 0.07495795975252475, 0.6260769403321403, 0.516023020022823, 0.12530743992648168, 0.1631851111231076, 0.5761299051739115, 0.32228765165198, 0.6383964021540309] ``` 这样,就可以生成包含20个随机数的列表。如果你想要生成指定范围内的随机数,可以使用random模块的其他函数,例如randint()函数和uniform()函数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值