Anyone here know C?

I need help creating a function that randomly produces a letter from the set (a,b,c)

I was thinking the rand function but I don't know how to apply it to characters, or constrain it to a smaller set of options.

Have letters in an array, then generate a random number which you use to determine which element you'll retrieve from that array?

we haven't learned arrays yet, so I don't think we should have to use them.

learn about mod(%) operator, and you should be fine.

OHH I forgot, search for the ASCII table.

random()*N and then remove the decimal part to get an integer in the range [0,N-1]

isn't that for remainders?

in the ACS table, a b and c are 97-99, so would i just set the function to select from those 3 numbers?

Are you guys retarded? Why has nobody posted valid code yet? I think a is 97 in ascii, so assuming that, you can do
char thing() { return (char)(((int)rand()*3)+97);}

make an array containing the three characters you want
generate a random number from 0 to 2
return character in the array index of that number

Quit 4ch and study, nobody will do you homework kid

testing

>rand()*3
Did you mean rand()%3?

what if I just generated a random number from 0 to 2 and associated each number 0,1,2 with a,b,c? unless that's basically the same as what you're saying

not asking anyone to do it, just for some guidance

C is for brainlets.

>le we must hate everything mainstream XD
kys

yes that is exactly what
though if your characters are in the same order as the alphabet then you can do the ascii value-thing too and just shift the random number to start at the relevant ascii value
the +97 in "shifts" the starting point of the random range
*3 scales the value of random(), which is between 0.0 and 1.0, to [0, 3)
taking the integer value makes it either 0, 1, or 2

I'm assuming it's really just a, b and c, since you mentioned that you haven't learned arrays yet. If it's supposed to handle any general case, then i don't know what the fuck your teacher is doing.

this is probably the easiest way for beginners.

You get the remainder of a random number to get a random number whose range of possible values is smaller. So for example, 10 % 3 will give you 0, 1 or 2 and you can easily convert that to get an output of a,b and c.

These are probably right and maybe even the best way to do it, but I'm not sure if OP has learned about casting given that he said he doesn't know about arrays yet. I'm pretty sure if I were the teacher I'd teach arrays first.

Just generate 0,1,2 random and use a case/if statement

for some reason the rand function keeps producing the same number every time i run it. is this expected?

I think C needs you to set the seed the random-function uses yourself (for instance using the current system clock), otherwise it uses some static system-dependent one or something.

idk how to do that in C

thanks

is this actually what intro CS people struggle with?

yes

srand(time(NULL));
Include time.h lib

then how are you representing the set in code

niggas really doing another niggas homework in this thread

I assume if num == 0 then "a" elif num == 1 then "b" ...

aha I nagged people at every tiny problem I had reading tutorials/documentation when I started out like 7 years ago to get where I am today so I mean sure maybe when someone is asking for help they are just trying to get done quickly but then again maybe not

no they aint nigguh

Yes they are, nigga:
#include
#include
#include
char my_function(int randominput){
if(randominput == 0)
return 'a';
if(randominput == 1)
return 'b';
if(randominput == 2)
return 'c';
return 'X';
}

int main(void)
{
time_t t;
srand((unsigned) time(&t));
int random_number = rand() % 3;
char displayed_letter = my_function(random_number);
printf("Testing %c\n", displayed_letter);
return 0;
}

#include
#include
#include

int main() {
srand(time(NULL));
printf ("%c\n", "abc"[rand() % 3]);
return 0;
}

See:
(FUNCTION)
And:
(NO ARRAYS)

But good job passing a function a NULL value to time. Not portable!

That's the most retarded fucking thing I've ever heard. Tell your teacher to fuck off and use an array like a rational individual.

What the fuck? No parameter in your main? So I guess we'll just pass it infinite parameters, eh? int main(void) or take proper arguments from the system

I am not OP but that is the absolutely easiest way to do it if you have barely touched any language features

You are allowed to omit parameter list in C. That code is perfectly fine and you are retarded.

what about
return rand() %3 + 'a'
?

Not sure this compiles, don't you need to cast the literal?

t. Dunning-Kruger

no, a char is just a pretty way to display an integer

welcome to C

it can be your best friend or your worst enemy depending on how new you are

GCC won't even warn about this with -Wmain. The definition of main is implementation-defined, and this is valid in GCC.

>The definition of main is implementation-defined
The definition of main is either
int main(void) or int main(int argc, char *argv[]) or anything which is equivalent to those (different argument names, compatible types.
An empty argument list for a function is compatible with (void), so int main() is a valid definition of main.

>not using C++ instead

>Using C++
Could you be any more of a shit-eater?

>not using haskell for web development
Could you be any more of a shit-eater?

>Could you be any more of a shit-eater?
By using C.

Empty parameter lists in a function declaration are obsolescent as of C99. Stop it.