1. <i id='u7VVG'><tr id='u7VVG'><dt id='u7VVG'><q id='u7VVG'><span id='u7VVG'><b id='u7VVG'><form id='u7VVG'><ins id='u7VVG'></ins><ul id='u7VVG'></ul><sub id='u7VVG'></sub></form><legend id='u7VVG'></legend><bdo id='u7VVG'><pre id='u7VVG'><center id='u7VVG'></center></pre></bdo></b><th id='u7VVG'></th></span></q></dt></tr></i><div id='u7VVG'><tfoot id='u7VVG'></tfoot><dl id='u7VVG'><fieldset id='u7VVG'></fieldset></dl></div>

    2. <legend id='u7VVG'><style id='u7VVG'><dir id='u7VVG'><q id='u7VVG'></q></dir></style></legend>

    3. <small id='u7VVG'></small><noframes id='u7VVG'>

      • <bdo id='u7VVG'></bdo><ul id='u7VVG'></ul>
        <tfoot id='u7VVG'></tfoot>

        如何在 C++ 中创建一个随机的字母数字字符串?

        时间:2023-06-05

      1. <i id='vSXhc'><tr id='vSXhc'><dt id='vSXhc'><q id='vSXhc'><span id='vSXhc'><b id='vSXhc'><form id='vSXhc'><ins id='vSXhc'></ins><ul id='vSXhc'></ul><sub id='vSXhc'></sub></form><legend id='vSXhc'></legend><bdo id='vSXhc'><pre id='vSXhc'><center id='vSXhc'></center></pre></bdo></b><th id='vSXhc'></th></span></q></dt></tr></i><div id='vSXhc'><tfoot id='vSXhc'></tfoot><dl id='vSXhc'><fieldset id='vSXhc'></fieldset></dl></div>

          <bdo id='vSXhc'></bdo><ul id='vSXhc'></ul>
              <tbody id='vSXhc'></tbody>
            <legend id='vSXhc'><style id='vSXhc'><dir id='vSXhc'><q id='vSXhc'></q></dir></style></legend>

              <small id='vSXhc'></small><noframes id='vSXhc'>

              <tfoot id='vSXhc'></tfoot>
                  本文介绍了如何在 C++ 中创建一个随机的字母数字字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想创建一个随机字符串,由字母数字字符组成.我希望能够指定字符串的长度.

                  I'd like to create a random string, consisting of alpha-numeric characters. I want to be able to be specify the length of the string.

                  我如何在 C++ 中做到这一点?

                  How do I do this in C++?

                  推荐答案

                  Mehrdad Afshari 的 answer 可以解决问题,但我发现它对于这个简单的任务来说有点过于冗长.查找表有时可以创造奇迹:

                  Mehrdad Afshari's answer would do the trick, but I found it a bit too verbose for this simple task. Look-up tables can sometimes do wonders:

                  #include <ctime>
                  #include <iostream>
                  #include <unistd.h>
                  
                  std::string gen_random(const int len) {
                      static const char alphanum[] =
                          "0123456789"
                          "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                          "abcdefghijklmnopqrstuvwxyz";
                      std::string tmp_s;
                      tmp_s.reserve(len);
                  
                      for (int i = 0; i < len; ++i) {
                          tmp_s += alphanum[rand() % (sizeof(alphanum) - 1)];
                      }
                      
                      return tmp_s;
                  }
                  
                  int main(int argc, char *argv[]) {
                      srand((unsigned)time(NULL) * getpid());     
                      std::cout << gen_random(12) << "
                  ";        
                      return 0;
                  }
                  

                  请注意,rand 生成质量较差的随机数.

                  这篇关于如何在 C++ 中创建一个随机的字母数字字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何简洁、便携、彻底地播种 mt19937 PRNG? 下一篇:Rand() % 14 只生成值 6 或 13

                  相关文章

                      <bdo id='6G07Q'></bdo><ul id='6G07Q'></ul>
                    <i id='6G07Q'><tr id='6G07Q'><dt id='6G07Q'><q id='6G07Q'><span id='6G07Q'><b id='6G07Q'><form id='6G07Q'><ins id='6G07Q'></ins><ul id='6G07Q'></ul><sub id='6G07Q'></sub></form><legend id='6G07Q'></legend><bdo id='6G07Q'><pre id='6G07Q'><center id='6G07Q'></center></pre></bdo></b><th id='6G07Q'></th></span></q></dt></tr></i><div id='6G07Q'><tfoot id='6G07Q'></tfoot><dl id='6G07Q'><fieldset id='6G07Q'></fieldset></dl></div>

                    1. <small id='6G07Q'></small><noframes id='6G07Q'>

                    2. <tfoot id='6G07Q'></tfoot>
                    3. <legend id='6G07Q'><style id='6G07Q'><dir id='6G07Q'><q id='6G07Q'></q></dir></style></legend>