Random char (a-z) withouth repeats

Hi guys, im doing a game, which needs to repeat at least 26 times, once for each letter of the abc, which needs to be random every time.

I got this, but is not working, it gives me random char, but they are repeating...

this is my code..

1
2
3
4
5
6
7
8
9
10
11
12
13
srand(time(NULL));
        var.letra = 97 + rand()%(122-97);

        for(i=0;i<27;i++){
            while(var.temp[i] == var.letra){
                var.letra= 97 + rand()%(122-97);
            }
        }
        for(i=0;i<=partidas;i++){
            if(i==partidas){
            var.temp[i] = var.letra;
            }
        }


thanks in advance
Last edited on
Save all letters to the array, shuffle it and just take letters in order.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <algorithm>
#include <array>
#include <iostream>
#include <numeric>

int main()
{
    std::array<char, 26> letters;
    std::iota(letters.begin(), letters.end(), 'a');
    for(char c: letters) //Array before shuffling
        std::cout << c;
    std::cout << '\n';
    std::random_shuffle(letters.begin(), letters.end());
    for(char c: letters) //After shuffling
        std::cout << c;
}
abcdefghijklmnopqrstuvwxyz
mbjyalzvepsfonxqgduritwkch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
	string x = "abcdefghijklmnopqrstuvwxyz";
	int inc = 26;
	while (1){

		if (inc < 1){
			cout << "end of blah blah";
			break;
		}
		int Number = rand() % inc + 0;
		cout << x.at(Number) << endl;

		
		string x2 = "";
		for (const auto c : x){

			if (c != x.at(Number)){

				x2.push_back(c);
			}
		}

		x = x2;
		--inc;
	}
Last edited on
thanks a lot both =)
Topic archived. No new replies allowed.