Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the random number generated by C++ programming in a specified range?

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you C++ programming to generate a specified range of random numbers is what, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article hope you can get something.

C _ Mini Program:

# include # include

/ * * get random number * return: random number * / int commonGetRandNumber (const int low, const int high) {int randNum = 0

/ / generate random number randNum = rand ()% (high-low + 1) + low

Return randNum;} # define RAND_MAX_LEN (16) # define RAND_MIN_VALUE (0) # define RAND_MAX_VALUE (9999) / * * get the string form of a random number * return: random number string * / std::string commonGetRandString () {int low = RAND_MIN_VALUE

Int high = RAND_MAX_VALUE; int randNum = 0

Char RandArray [RAND _ MAX_LEN] = {0}; std::string randStr

/ / generate random number srand (time (0))

RandNum = commonGetRandNumber (low, high)

Snprintf (randArray, sizeof (randArray)-1, "% d", randNum)

RandStr = randArray; return randStr

}

/ * * get to generate a random string based on a given character array and random number * /

Std::string getNonceStr (int length = 32) {std::string chars = "abcdefghijklmnopqrstuvwxyz0123456789"

Std::string str = ""

/ / generate random number srand (time (0))

For (int I = 0; I < length; iTunes +) {str + = chars.substr (commonGetRandNumber (0, chars.size ()-1), 1)

} return str;} int main () {

/ / get the random number srand (time (0))

Int randNum = commonGetRandNumber (10,100)

Printf ("randNum=%d\ n", randNum)

/ / get the string form of a random number std::string randStr = commonGetRandString ()

Printf ("randStr=%s\ n", randStr.c_str ())

/ / gets to generate a random string std::string randChar = getNonceStr () based on a given character array and random number

Printf ("randChar=%s\ n", randChar.c_str ();}

A call to rand () produces a random number between [0rem 32757], and the absolute value of (high-low) cannot exceed 32767.

The above content is C++ programming to generate a specified range of random numbers, have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report