Home > front end >  how do i randomly select an element in an array?
how do i randomly select an element in an array?

Time:03-27

So I've been trying to do this for an embarrassingly long time now. First of all here is my (very bad) code:

char firstInfection() {            
     char cities[] = {"aa", "bb", "cc", "dd", "ee", "ff"};            
     srand(time(NULL));            
     int randnum = rand() % 5;            
     char firstCity = cities[randnum];            
     return firstCity; 
}

I didn't include the preprocessor stuff but I'm also using string, math and time (obviously stdio and stdlib too)

Line 2 errors out here, saying: error: too many initializers for ‘char []’ I'm not really sure what's going on here.

I'm having trouble understanding some of the things I need to do. I was told on tutorialspoint that I have to initialize random with line 3: srand(time(NULL)) I'm not sure why lol.

I tried shortening the names of the elements in cities[]

I'm not really sure what else to do try. I'm guessing that the whole thing is completely wrong but I really don't know.

CodePudding user response:

#include <stdio.h>

const char* firstInfection() {            
     static const char* cities[] = {"aa", "bb", "cc", "dd", "ee", "ff"};            
     srand(time(NULL));            
     int randnum = rand() % 6;  // Select value 0 to 5 (inclusive)            
     const char* firstCity = cities[randnum];            
     return firstCity; 
}

int main(void) {
    
    printf("Random String: %s\n", firstInfection());
    return 0;
}

CodePudding user response:

Answer given by @abelenky it not that good as he/she is calling srand() function inside a function which is not main(). NOTE: srand() should be called only once in any program within the main() function.

Some Improvements:

  • Instead to using bare return 0;, use return EXIT_SUCCESS;, which is defined in the header file stdlib.h.
  • Data type char means a single character which is enclosed within single quotes '', use char * (c-string) to store multiple characters.
  • Always use size_t to store indexes of any arrays, instead of int
  • Only C If your function is not taking any arguments then write it like this int func(void) { }, here's why.
  • Instead of using bare rand() % 5, get the size of the array stored on stack memory using sizeof() operator
  • Mark your local variable cities as static because you are returning it too.
  • Your function firstInfection() should be declared with static linkage.
  • You are calling time() function which is declared in time.h header file, so include that too

Final Code:

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

static const char *firstInfection(void)
{
    static const char *cities[] = {"aa", "bb", "cc", "dd", "ee", "ff"};
    size_t rnd_index = rand() % (sizeof(cities) / sizeof(*cities));
    return cities[rnd_index];
}

int main(void)
{
    srand(time(NULL));
    printf("%s\n", firstInfection());
    return EXIT_SUCCESS;
}

Output:

[tushar@tushar-arch Desktop]$ ./main
aa
[tushar@tushar-arch Desktop]$ ./main
ee
[tushar@tushar-arch Desktop]$ ./main
cc
  • Related