#rand() help
29 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
Sorry if that doesn’t make sense
I want to associate a word with each random integer, and print that instead of the integer
Obviously doing int 1 = “Dog”; doesn’t work
create an array of string literals generate a random index and print whatever word in said index
Ok so the directions are to print the word in a separate function named printImage(). “Print the name of an image when passed its number as an argument”
i don't see how that changes things
Idk what to do with that
So I’d create an array of every word I want to use, and printing a word for whatever random integer is selected from the array position?
well... yes
@subtle trout Has your question been resolved? If so, type !solved :)
consider:
static int generate_rand(int max, int min) {
...
}
static void foo(char **words, size_t size) {
if (!words || !size) return;
size_t idx = (size_t)generate_rand(4, 0);
assert(idx < size);
puts(words[idx]);
}
int main(void) {
// seed your rand here
char *words[] = {"1", "2", "3", "4"}; // this array can also be local within foo if you so desire
foo(words, sizeof words / sizeof *words);
}```
or something along these lines
Just figured it how to do it with if statements
thats also an option (though not as flexible)
if you are taking the this approach - you might want to rethink your design here. if getRandomInteger well, gives you a random integer - does it needs to print the word associated with said integer too?