Random character generator not working

Oct 14, 2013 at 11:32am
Ok, so my random character generator is not working properly. The letter[100] is initialized at the start and they are liked that by default. I've also used the srand once already, I'm not sure if I need to use it again. I actually don't know how it works but I know what it does. The goal of the function is to put 7 random char from the letter array to the players.tiles struct. If the letter is already taken in the letter array, the position of that letter in the array will be null. I hope you understand.

The error is actually it keeps printing random chars, not even letters. And it doesn't change too like the srand is not there
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  char letter[100] = {'A','A','A','A','A','A','A','A','A','B','B','C','C','D','D','D','D','E','E','E','E','E','E','E','E','E','E','E','E','F','F','G','G','G','H','H','I','I','I','I','I','I','I','I','I','J','K','L','L','L','L','M','M','N','N','N','N','N','N','O','O','O','O','O','O','O','O','P','P','Q','R','R','R','R','R','R','S','S','S','S','T','T','T','T','T','T','U','U','U','U','V','V','W','W','X','Y','Y','Z',' ',' '};


  void player_tiles(games players[4], int no, char letter[100], int i){
	int j, rd;
	printf("Player %d's Tiles: {", i);
	for(j=1;j<=7; j++){
		if(players[i].tiles[j] == NULL){
				do{
					srand(time(NULL));
					rd=rand()%99;
				}while(letter[rd]==NULL);
				players[i].tiles[j] = letter[rd];
                                letter[rd]==NULL;
			}
			if(j!=7){
				printf("%c, ", players[i].tiles[j]);
			}else printf("%c", players[i].tiles[j]);
	}
	printf("}\n");
		
}
Last edited on Oct 14, 2013 at 11:34am
Oct 14, 2013 at 11:43am
Actually I've use rand already and it works properly but this one doesn't. The only difference is that I used a variable "rd" for this one.
Oct 14, 2013 at 11:46am
srand(time(NULL));
This should be done only once in your program. This randomizes the generator using the present time as seed.

Also I do not understand the following statements:
1
2
3
4
5
6
do{
       srand(time(NULL)); // move this statement to the beginning of your program.
       rd=rand()%99;
} while(letter[rd]==NULL); // letter[rd] is a character. 
players[i].tiles[j] = letter[rd];
letter[rd]==NULL; // This is not a statement. 


Try changing it to:
1
2
rd=rand()%99;
players[i].tiles[j] = letter[rd];
Last edited on Oct 14, 2013 at 12:12pm
Oct 14, 2013 at 11:49am
time(NULL) reads from your computers clock to seed the randomizer, so by calling it in
each iteration of your loop it's seeding the same thing over and over. You only need to srand(time(NULL) once in your program.
Last edited on Oct 14, 2013 at 11:49am
Oct 14, 2013 at 12:09pm
Ok, I got one.

But my random char generator is not working.
It worked properly the 1st time at the main but when I call the one on the function it's giving me random characters and its like the computer's clock is not seeding the randomizer.

Here's the one on the main:
 
board[12][7]=letter[rand()%99];


And here's now the function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void player_tiles(games players[4], int no, char letter[100], int i){
	int j, rd;
	printf("Player %d's Tiles: {", i);
	for(j=1;j<=7; j++){
		if(players[i].tiles[j] == NULL){
				do{					
					rd=rand()%99;
				}while(letter[rd]==NULL);
				players[i].tiles[j] = letter[rd];
				letter[rd]==NULL;
			}
			if(j!=7){
				printf("%c, ", players[i].tiles[j]);
			}else printf("%c", players[i].tiles[j]);
	}
	printf("}\n");
		
}
Oct 14, 2013 at 12:12pm
Please try the suggestions in my earlier post.
Oct 14, 2013 at 12:16pm
It's in a do while so it can check whether the one inside the letter[rd] is null. If it's null, it will get another random number. So I need that part.

It's a scrabble.
The letter array is the tiles and the players.tiles is of course the players' 7 tiles.
Last edited on Oct 14, 2013 at 12:18pm
Oct 14, 2013 at 12:19pm
Ok but how about this "statement"
letter[rd]==NULL;

You should change it to
letter[rd]=NULL;

EDIT: Also, instead of NULL, I would prefer some other unused but known character to put the value of letter[rd]. eg. '%', '&', '@', '!', '>', '<'
Last edited on Oct 14, 2013 at 12:22pm
Oct 14, 2013 at 12:23pm
Oh, I didn't see that one. But it still doesn't work. Do you see the difference bet. the one on my main func. and the one on my player_tiles function?

Do you think it has something to do with it?

The rand command is in a variable on the playertiles function but on the main it isn't.
Oct 14, 2013 at 12:32pm
No, I need NULL to empty the spot in the array.

Last edited on Oct 14, 2013 at 1:11pm
Oct 14, 2013 at 1:23pm
What do you mean by empty the spot? When you assign a null character, the array location is filled with the byte associated with null character.

I don't have much experience with c but I haven't seen any char array that has a null character in between, that too possibly more than 1.
Oct 14, 2013 at 1:26pm
I know where I'm getting the problem now, it's coming from this condition "players[i].tiles[j] == NULL". Do you know how to check if the spot in the array is empty or you know hasn't been declared.

I'm trying to empty it the letter[rd] by turning it to NULL. But I'm not sure this is the right way to do it. Do you know how?
Oct 14, 2013 at 1:41pm
I think I know how to fix it I just need to know how to initialize an array in a struct with null values. It's not statisfying the condition that I gave.
Oct 14, 2013 at 5:22pm
You need to remember, among other things, that valid array indices are 0 to array_size-1 where array_size is the number of elements in the array, and not 1 to array_size.

Here's some C code for your to peruse:
http://ideone.com/62WPqC

Note that this is a C++ forum. You might find a C forum fits better with your needs.
Topic archived. No new replies allowed.