mix up a string of arrays

Is there any easy method to randomly mix up a char[]?
example:
input:truck
output: crkut, tkcur or crukt...
Last edited on
closed account (zwA4jE8b)
permutations. I don't know the algorithm but there was a post similar to this but with numbers.

http://www.cplusplus.com/forum/general/44552/
Last edited on
closed account (zwA4jE8b)
a random number generator that generates numbers only up to the size of the string. so truck 0-4,

mixup[0] = string_variable[randnumber]
repeat
check for collisions
closed account (yUq2Nwbp)
i can offer you somethin like this

#include<stdlib>
#include<ctime> // it could be <time> or <time.h>

char x[20];
std::cin>>x;

int l = strlen(x);
for(int i=0;i<strlen(x)-1;i++)
x[i]=x[ rand%(strlen(x)-2)];

this is not very good example because elements could possibly be repeated.....but you can amend it......for example create char y[20] and in each step input in it x[ rand%(strlen(x)-2)].......then in each step check wheter your new x[ rand%(strlen(x)-2)] already exists in y[20] or not....in this way you can do what you want but that also isn't the best way to solve problem...
@creativeMFS

im doing this in a project. In a 3D char array that can have hundreds of different strings, if i do it that way its gonna be really uneficient, and consume lots of time, what i was looking for was to somehow just input a string and get a mixed up string, if i mix a char 1 by 1, its gonna be many thousands of iterations.

In worst case i can have 5000 words with an average of 7 letters each making it 35000 iterations plus the collisons
Last edited on
@david91

to avoid repetitions i thought it could be:

void func(char *x){
int len = strlen(x)
char y [len + 1] = NULL;

for(int j, i=0; i < len;i++){
do{
j = rand()%len;
}while(y[j] != '0');
y[j] = x[i];
}

the problem is this code can go on forever if i have bad luck and one position of the string never comes out

since i might do this 35000 times probably it will happen alot
Last edited on
closed account (yUq2Nwbp)
i 'll try to do it.....if i could i unconditioned will reply here....
1
2
3
4
5
6
7
8
#include <cstring>
#include <algorithm>

// ...

char car[] = "Truck";
size_t s = strlen(car);
std::random_shuffle(car, car + s);

Untested!

Better to use a std::string.
thankx caligula.

tested, it works perfectly.

just one problem the shuffle is always the same, it always shuffle the same way, in that example the output is always "krcut"

have any ideas how i can make it be possible to come out in all ways? like tuckr and ucrkt.

the problem is im doing this in a word game, so if someone figured out the algorithm, would be easy to beat it.
Last edited on
i thought of an idea, what if i change x (x being random) random letters with others from same word.

example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
srand(time(NULL));

char car[] = "truck";
int len = strlen(car);

int x = random()%len;

char aux;
int letter1, letter2;

for(int i = 0; i < x; i++){
    letter1 = random()%len;
    letter2 = random()%len;
    aux = car[letter1];
    car[letter1] = car[letter2];
    car[letter2] = aux;
}
    


think this might be the best idea, pretty efficient, all possibilities, having the same chance.
Last edited on
closed account (yUq2Nwbp)
you are absolutely right antonio craveiro.....just input

#include<cstdlib>
#include<ctime>

and then you can write srand(time(0))...
i tried this and sometimes, i knew this would happen it came truck has the output, so ill just put a clause and use caligulas idea if that happens.
thx every1 for the help, would have taken way more time alone.
@antonio

I don't know which PRNG your STL uses. Maybe a simple
srand(time(NULL));
in the beginning will solve the problem.
@caligula

ah, didnt know it worked with srand as well, thx, that will make my code way simpler
for any1 that has same problem as me in the future here's the best solution i found thanks to every1 in here specially Caligulaminus

1
2
3
4
5
6
7
8
9
#include <cstring>
#include <algorithm>
#include <time.h>

void mix_string(char *string){
  srand(time(NULL));

  std::random_shuffle(string, string + strlen(string));
}
Last edited on
You shouldn't call srand() in your mix_string(). Call it once at the beginning of your main().
Topic archived. No new replies allowed.