Discuss Scratch
- Discussion Forums
- » Advanced Topics
- » cpp random string generation
- itsAMeA
-
Scratcher
36 posts
cpp random string generation
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
static const char alphanum =
“0123456789”
“!@#$%^&*”
“ABCDEFGHIJKLMNOPQRSTUVWXYZ”
“abcdefghijklmnopqrstuvwxyz”;
int stringLength = sizeof(alphanum) - 1;
char genRandom() // Random string generator function.
{
return alphanum;
}
int main()
{
srand(time(0));
for(int z=0; z < 21; z++)
{
cout << genRandom();
}
return 0;
}
next up: convert c++ to python
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
static const char alphanum =
“0123456789”
“!@#$%^&*”
“ABCDEFGHIJKLMNOPQRSTUVWXYZ”
“abcdefghijklmnopqrstuvwxyz”;
int stringLength = sizeof(alphanum) - 1;
char genRandom() // Random string generator function.
{
return alphanum;
}
int main()
{
srand(time(0));
for(int z=0; z < 21; z++)
{
cout << genRandom();
}
return 0;
}
next up: convert c++ to python
- TheAspiringHacker
-
Scratcher
100+ posts
cpp random string generation
- You are setting a const char to a string?? Did you mean const char*?
- If you're using C++, please use std::string instead of const char* anyway.
- Your genRandom function just returns the alphanum string (and I'm not sure if the fact that it returns a char is due to the type error from before or if it is intentional).
- Try to avoid global variables. Can you define alphanum in the genRandom function instead?
EDIT: When I try to start the list from 0, the BBCode just renders as bullet points???
EDIT 2: Ah nevermind, you index alphanum using randomness, but BBCode hid the brackets. You know, if you use std::string, you don't need an extra variable for the string's length.
Last edited by TheAspiringHacker (June 15, 2018 15:34:50)
- MegaApuTurkUltra
-
Scratcher
1000+ posts
cpp random string generation
In python this is just
import random, itertools, string def chargen(): while True: yield random.choice(string.printable) def random_chars(n): return ''.join(itertools.islice(chargen(), n)) print(random_chars(21))
- Wettining
-
Scratcher
500+ posts
cpp random string generation
In python this is justbut… it's not C++import random, itertools, string def chargen(): while True: yield random.choice(string.printable) def random_chars(n): return ''.join(itertools.islice(chargen(), n)) print(random_chars(21))
- Wettining
-
Scratcher
500+ posts
cpp random string generation
You could do this in Rust and you'll see that there aren't many differences 

use std::rand;
use std::rand::Rng;
use std::str;
static TARGET: &'static str = "rust is better";
static ALPHABET: [char, ..(Put your amount of characters here)] = [(Put your characters here)]; //sorry I'm lazy
fn random_char() -> char {
rand::rng().choose(ALPHABET)
}
fn builder(push: &fn(v: char)) {
for _ in range(0,TARGET.len()) {
push(random_char());
}
}
fn random_string () -> ~str {
let result = std::vec::build(Some(TARGET.len()), builder);
str::from_chars(result)
}
fn main() {
println(random_string());
}
- MegaApuTurkUltra
-
Scratcher
1000+ posts
cpp random string generation
?In python this is justbut… it's not C++
next up: convert c++ to python
- Wettining
-
Scratcher
500+ posts
cpp random string generation
That seems like a downside on all fronts though doesn't it??In python this is justbut… it's not C++next up: convert c++ to python
Especially with Python being used for more abstract things and C++ for more direct things on the computer hardware
- Discussion Forums
- » Advanced Topics
-
» cpp random string generation