954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

help on rand() srand() pls.

Ok, I need to make a program that randomly generates 4 random letters from 12 random letter and it cannot reapeat the same letter. for example you can only pick from a,b,c,d,e,f,g,h,i,j,k,l and the program can randomly generate a,b,c,d or b,c,d,e but should not generate a,a,b,b or something that repeats.Another is that a letter represents 1 animal. I'm new to rand() and srand() so any links on rand() srand() tutorial may help too. :)

#include<stdio.h>
#include<stdlib.h>
int main()
{
    char a=ant,b=bear,c=cat,d=dog,
    e=emu,g=goat,l=lizard,m=monkey,
    p=parrot,r=rabbit,s=snake,t=tiger;
    int i;
        for(i=1,i<=4,i++)
        {printf("%c %c %c %c",&i &i &i &i);
        system("pause");
        return 0;
        }

so far I made these but I don't know how and where to add rand and srand()and it ca't be compiled as well. I'm not sure if the a=ant thing is correct as well.

ineedsomehelp:3
Newbie Poster
24 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

documentation
The char a = ant; is only valid if ant is a character defined at some other location (unlikely in your code example). A char variable can only hold values of a single character ( 'a', 'b', 'c' for example). In your case it might be better to investigate enumerations ( enum ) as it will allow a name to be associated to an integer value as you seem to want:

enum animal { ANT = 0, BEAR, CAT, DOG, ... };
L7Sqr
Practically a Master Poster
657 posts since Feb 2011
Reputation Points: 201
Solved Threads: 124
 

If you wanna store the names like a = ant you should use strings (char*).

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

#define sz 12

int main()
{
	int i, j;
	char* animals[sz] = {"ant", "bear", "cat", "dog", "emu", "goat", "lizard", "monkey", "parrot", "rabbit", "snake", "tiger"};
	char* chosen[4] = {"", "", "", ""};
	srand(time(NULL));

	for( i = 0; i < 4; i++ )
	{
		chosen[i] = animals[rand()%12];
		for( j = 0; j < 4; j++ )
			if( strcmp(chosen[i], chosen[j]) == 0 && j != i ) //checks to see if the animal picked is unique
			{
				i--; //if not decrease counter so it overwrites the last animal
				break;
			}
	}

	for( i = 0; i < 4; i++ )
	{
		printf("%c - ", chosen[i][0]); //for first letter of animals name
		printf("%s  ", chosen[i]); //for whole name of animal
	}
	printf("\n");


	return 0;
}
sfuo
Practically a Master Poster
656 posts since Jul 2009
Reputation Points: 164
Solved Threads: 99
 

Thanks a lot! :) I'm new on C programming just studied about If-Else and Switch and for and I read about some rand() and srand() but I don't seem to get it.Can you suggest any links that might teach those codes you just written cause I want to learn to make programs like those too. Anyways I really really appreciate your help! :)

ineedsomehelp:3
Newbie Poster
24 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

I actually started with C++ so I learned from http://www.cplusplus.com/doc/tutorial/ but learning loops, variables and arrays is common between the two of them.

sfuo
Practically a Master Poster
656 posts since Jul 2009
Reputation Points: 164
Solved Threads: 99
 

THANKS A LOT~! :) the link helped me a lot :)

ineedsomehelp:3
Newbie Poster
24 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: