返回随机字符串

Returning a random string

本文关键字:字符串 随机 返回      更新时间:2023-10-16

我正在尝试使用"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+1rand()%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不被认为是一个好习惯,您可能需要谷歌以获取更多信息。