我是靠谱客的博主 标致招牌,最近开发中收集的这篇文章主要介绍[Functions]D. Liang 5.18 Generating random characters.cDescriptionHint,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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]: %dn", 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

最后

以上就是标致招牌为你收集整理的[Functions]D. Liang 5.18 Generating random characters.cDescriptionHint的全部内容,希望文章能够帮你解决[Functions]D. Liang 5.18 Generating random characters.cDescriptionHint所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(55)

评论列表共有 0 条评论

立即
投稿
返回
顶部