返回随机字符串
Returning a random string
我正在尝试使用"ABCDEFG"或更少的"ABCDEFG"制作一个随机字符串(取决于难度(将成为游戏((,但我的代码不断给我错误,例如"多重定义",首先在这里定义"。
我在C++方面的经验太少(以前只在 Java 中工作,并且应用程序发明家..( 所以我想我在这里缺少一些关于字符串/字符/函数的 c++ 基本规则。
这是我的代码:
#include "Functions.h"
#include <iostream>
#include <cassert>
#include <iterator>
#include <string>
#include <ctime>
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
string createRandomString(int);
int main(){
string test;
test=createRandomString(1);
cout << test;
return 0;
}
string createRandomString(int stringlength){
srand(time(NULL));
string lettersToUse="ABCDEFG";
string newOne="";
for(int i=0;i<=stringlength-1;i++){
srand(time(NULL));
newOne=newOne+lettersToUse[rand() % 7 + 1];
}
return newOne;
}
非常感谢您的帮助!
为什么包含这么多不需要的头文件?这些应该足够了:
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
您应该避免using namespace std;
.如果你不想每次都输入std::string
,特别是你可以使用using std::string;
using std::string;
using std::cout;
using std::endl;
现在进入您的函数。
为了避免编译器警告,我建议将srand(time(NULL));
更改为srand(static_cast<unsigned int>(time(NULL)));
你的循环是可以的,但你通常不写i <= stringlength - 1
而是i < stringlength
下一个srand(time(NULL));
是不必要的。
真正的问题是您的随机值:您正在使用rand()%7+1
。 rand()%7
会给你一个范围 [0,6] 的值,这是可以的,但是通过添加1
范围移动到 [1;7],7
超出了你的lettersToUse
字符串。
为了安全起见,您应该将范围限制为实际可访问的范围。 在您的情况下,lettersToUse.size()
是 7。正如我们之前看到的rand()%7
为您提供正确的范围。所以只要lettersToUse.size()
不0
,就永远可以rand()%lettersToUse.size()
。
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using std::string;
using std::cout;
using std::endl;
string createRandomString(int);
int main() {
string test;
test = createRandomString(1);
cout << test;
return 0;
}
string createRandomString(int stringlength) {
srand(static_cast<unsigned int>(time(NULL)));
string lettersToUse = "ABCDEFG";
string newOne = "";
for (int i = 0; i < stringlength - 1; i++) {
newOne += lettersToUse[rand() % lettersToUse.size()];
}
return newOne;
}
如果您尝试修改lettersToUse
甚至允许用户指定lettersToUse
,您应该明确添加对letterToUse
大小的检查,因为程序在为空时会崩溃。
如果您可以使用 C++11,我建议您使用它提供的C++随机数生成器:
#include <iostream>
#include <string>
#include <random>
using std::string;
using std::cout;
using std::endl;
string createRandomString(int);
int main() {
string test;
test = createRandomString(5000);
cout << test;
return 0;
}
string createRandomString(int stringlength) {
string lettersToUse = "ABCDEFG";
string newOne = "";
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(0, lettersToUse.size()-1); //Range from [0-6], when size is 7: 0-6 = 7 elements
for (int i = 0; i < stringlength - 1; i++) {
newOne += lettersToUse[distribution(generator)];
}
return newOne;
}
根据大写字母的建议,这里是使用std::generate_n
的版本:
#include <iostream>
#include <string>
#include <random>
#include <algorithm>
#include <iterator>
using std::string;
using std::cout;
using std::endl;
string createRandomString(int);
int main() {
string test;
test = createRandomString(1);
cout << test;
return 0;
}
string createRandomString(int stringlength) {
string lettersToUse = "ABCDEFG";
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(0, lettersToUse.size() - 1); //Range from [0-6], when size is 7: 0-6 = 7 elements
string newOne;
newOne.reserve(stringlength);
std::generate_n(std::back_inserter(newOne), newOne.capacity(), [&lettersToUse, &distribution, &generator]() { return lettersToUse[distribution(generator)]; });
return newOne;
}
循环中,srand(time(NULL));
每次都给你相同的随机值。#include "Functions.h"
是用来做什么的?我也删除了头文件。
这是您的完整代码:此函数生成一个随机字符串。
#include <iostream>
#include <cassert>
#include <iterator>
#include <string>
#include <ctime>
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
string createRandomString(int);
int main(){
string test;
test=createRandomString(6);
cout << test;
return 0;
}
string createRandomString(int stringlength){
srand(time(NULL));
string lettersToUse="ABCDEFG";
string newOne="";
for(int i=0;i<=stringlength-1;i++){
newOne=newOne+lettersToUse[rand() % lettersToUse.length()];
}
return newOne;
}
输出: 二分之三
关于你的错误,我什么也说不出来,因为你没有提到它们。关于如何创建一个随机字符串,这里已经问过了,我粘贴了我认为最简单的 Ates Goral 代码:
void gen_random(char *s, const int len) {
static const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < len; ++i) {
s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
}
s[len] = 0;
}
关于代码的几件事:
- 您使用大量 C 库。这是您应该尽可能避免的事情 IMO - 您处于C++,因此请使用C++功能。例如,C++中有一个
random
标题 ->看看这里 - 当您包含 C 库时,请注意您包含的版本。在C++中,您有两个选项,XXX.h 标头(例如
math.h
(,除非有非常特殊的原因,否则您通常不应使用,以及 cXXXX 版本(例如cmath
(,这是您通常应该使用的那个。前者是原样的 C 库,后者是针对C++使用而优化的 C 库。它正确使用命名空间并具有微妙的实现差异等。一般来说,对于后者,您可以保证原始 C 标头不一定有。
以 - 您的方式
use namespace std
不被认为是一个好习惯,您可能需要谷歌以获取更多信息。
- 字符串-C++后显示的随机字符
- 在c++构造函数中使用随机字符串生成器
- 使用std::mt19937从字符串中返回一个随机单词
- 尝试使用C++中的模板进行BST排序,但在使用随机字符串进行测试时不起作用
- 我的求解(字符串 a、字符串 b)的输出与随机哈希中的预期输出不匹配
- 将字符串转换为 int 后的随机值
- 我可以比朴素哈希表更快地将随机字符串映射到两个类吗?
- C++读取大文件并将其保存到字符串中,然后删除特定的随机单词
- 提取字符串中的随机单词
- 选择随机字符串数组的排序
- 生成随机长度的数字字符串
- 随机字符,从强制转换 Arduino 字符串到 Excel BSTR 通过 c++ DLL
- 这个随机字符串打印代码有什么问题?
- 尝试以随机字符(长读取)打印字符串返回结果
- 随机添加到字符串C++的空字符
- C++随机名称生成器给出重复字符串
- 如何生成伪随机 32 字节字符串以用作加密哈希函数中的盐?
- 队列快速排序不排序和打印随机字符串[C ]
- 随机字符串生成器不编辑我的字符串数组 (C++)
- 在长字符串(随机位置)中的C char搜索