[Functions]D. Liang 5.18 Generating random characters.c

Description

Write a header file that contains the following functions:

// Generate a random lowercase letter
char getRandomLowerCaseLetter()

// Generate a random uppercase letter
char getRandomUpperCaseLetter()

// Generate a random digit character
char getRandomDigitCharacter()

// Generate a random character
char getRandomCharacter()

Hint

You should submit the implementation of the function but do not submit the main() function.

The title provides the following header:

#ifndef SOURCE_H 
#define SOURCE_H

char getRandomUpperCaseLetter();
char getRandomLowerCaseLetter();
char getRandomDigitCharacter();
char getRandomCharacter();

#endif

My code:

// Generate a random lowercase letter
char getRandomLowerCaseLetter()
{
    return 'a' + rand()%26;         //英文字母有26个,随机数就在0到25之间找 
}
 
// Generate a random uppercase letter
char getRandomUpperCaseLetter()
{
    return 'A' + rand()%26;        //情况类似,将小写换成大写即可  
}
 
// Generate a random digit character
char getRandomDigitCharacter()
{
    return '0' + rand()%10;     //单个数字在0到9之间找 
}
 
// Generate a random character
char getRandomCharacter()
{
    int n =rand()%52;         //区分大小写 
    if( n < 26 )
        return 'A' + n;      //大写字母比小写字母序号要小 
    else
        return 'a' + n - 26;
}

rand

C Numerics Pseudo-random number generation
Defined in header <stdlib.h>

int rand();

Returns a pseudo-random integer value between ​0​ and RAND_MAX (0 and RAND_MAX included).
srand() seeds the pseudo-random number generator used by rand(). If rand() is used before any calls to srand(), rand() behaves as if it was seeded with srand(1). Each time rand() is seeded with srand(), it must produce the same sequence of values.
rand() is not guaranteed to be thread-safe.

Parameters

(none)

Return value

Pseudo-random integer value between ​0​ and RAND_MAX, inclusive.

Notes

There are no guarantees as to the quality of the random sequence produced. In the past, some implementations of rand() have had serious shortcomings in the randomness, distribution and period of the sequence produced (in one well-known example, the low-order bit simply alternated between 1 and 0 between calls). rand() is not recommended for serious random-number generation needs, like cryptography.
POSIX requires that the period of the pseudo-random number generator used by rand be at least
2 32 2^{32} 232.
.

POSIX offered a thread-safe version of rand called rand_r, which is obsolete in favor of the drand48 family of functions.

Example

Run this code

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
int main(void)
{
    srand(time(NULL)); // use current time as seed for random generator
    int random_variable = rand();
    printf("Random value on [0,%d]: %d\n", RAND_MAX, random_variable);
 
    // roll a 6-sided die 20 times
    for (int n=0; n != 20; ++n) {
        int x = 7;
        while(x > 6) 
            x = 1 + rand()/((RAND_MAX + 1u)/6); // Note: 1+rand()%6 is biased
        printf("%d ",  x); 
    }
}

Possible output:

Random value on [0,2147483647]: 448749574
3 1 3 1 4 2 2 1 3 6 4 4 3 1 6 2 3 2 6 1

Reference:https://en.cppreference.com/w/c/numeric/random/rand

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值