C++ 随机字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25815103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:26:19  来源:igfitidea点击:

C++ random char

c++randomchar

提问by juekvwei

I'm trying to generate a random char like this:

我正在尝试生成这样的随机字符:

char* test = "myname" + GenerateRandomChar();

I'm trying to do it without using string. So please don't say use string. Thanks

我试图在不使用字符串的情况下做到这一点。所以请不要说使用字符串。谢谢

回答by pm100

if you are writing c code (which it seems you are) then do

如果你正在编写 c 代码(看起来你是)那么做

char buff[8]; //sizeof("myname") + 1 + 1
sprintf(buff,"myname%c", GenerateRandomChar());

I am assuming that GenerateRandomChar exists and works

我假设 GenerateRandomChar 存在并且有效

回答by Remy Lebeau

Try something like this:

尝试这样的事情:

char[] test = "myname
char GenerateRandomChar()
{
    return (char) (random(127-33) + 33);
}
"; // note char[] instead of char* test[6] = GenerateRandomChar();

#include <cstdio>
#include <iostream>

char getRandomChar(){
    static char c = 'A' + rand()%24;
    return c;    
    }

int main ()
{
    srand((unsigned)time(0));
    char str[] = "test";
    char buffer[sizeof(str)+1];
    char rnd = getRandomChar();
    sprintf(buffer, "%s%c",str,rnd);
    std::cout << buffer << std::endl;
}

回答by Javi V

You can add a sizeof to the buffer so it is char length independent:

您可以将 sizeof 添加到缓冲区,使其与字符长度无关:

"myname" + c

Note that the getRandomChar()function only gives uppercase letters. You will need to it a bit more sofisticated if you want also lowercase.

请注意,该getRandomChar()函数仅提供大写字母。如果你还想要小写,你需要更复杂一些。

This solution assumes you know the size of the initial string in compilation time.

此解决方案假设您知道编译时初始字符串的大小。

回答by Javi V

Your code as it stands is incorrect for two reasons:

由于两个原因,您的代码不正确:

  1. You're performing a deprecated conversion from const char*to char*. It should not compile.
  2. You're performing pointer arithmetic, not concatenation.
  1. 您正在执行从const char*到的弃用转换char*。它不应该编译。
  2. 您正在执行指针算术,而不是串联。

The following expression:

以下表达式:

char *ptr = "myname";
char c = ' '; // ASCII equivalent is 32
int n = (int) c;
char *s = ptr + n; // s now points out of bounds of ptr

where cis some chardoes not perform concatenation, but rather pointer arithmetic. It's similar to:

where cis somechar不执行连接,而是执行指针运算。它类似于:

#include <cstdlib>
#include <cstring>
#include <iostream>

char foo() {
  // return a random character here
}

int main() {
  const char* myname = "myname";
  // enough room to contain new character
  // and null terminator
  char* buffer = new char[strlen(myname) + 2];
  // strcpy includes the null terminator
  strcpy(buffer, myname);
  // We overwrite myname's null terminator
  buffer[strlen(myname)] = foo();
  buffer[strlen(myname) + 1] = '##代码##';
  std::cout << buffer;
  delete[] buffer;
}

If you insist on doing C-style code, then you need to use the standard library functions strcpyand possibly strcat. Example:

如果您坚持使用 C 风格的代码,那么您需要使用标准库函数strcpy,可能还需要使用strcat. 例子:

##代码##