2

How can I select a random element from a character array in c ?

For instance:

char *array[19];
array[0] = "Hi";
array[1] = "Hello";

etc

I am looking for something like array[rand], where rand is the random integer number between o and the array's length(in this case 20) like 1, 2, 3 , 19 etc.

asked Jun 20, 2013 at 13:44
16
  • 1
    You're looking after rand() Commented Jun 20, 2013 at 13:46
  • 2
    maybe char* array[20]; Commented Jun 20, 2013 at 13:47
  • 1
    So, to be clear, 20 * rand() is not correct. Use rand() % 20. However, if your array is 20 elements long you need to fix your variable declaration! You're only allocating 19 elements there. Commented Jun 20, 2013 at 13:51
  • 1
    @svk No, don't use % because the results can be non-uniform. Multiplying a U(0,1) value times 20 and flooring will give an int from 0 to 19, i.e., 20 elements all set for zero-based array indices. Commented Jun 20, 2013 at 13:54
  • 1
    What @pjs is refereing to is this issue from the linux man pages: "The versions of rand() and srand() in the Linux C Library use the same random number generator as random(3) and srandom(3), so the lower-order bits should be as random as the higher-order bits. However, on older rand() implementations, and on current implementations on different systems, the lower-order bits are much less random than the higher-order bits. Do not use this function in applications intended to be portable when good randomness is needed. (Use random(3) instead.)" Commented Jun 20, 2013 at 13:59

5 Answers 5

3

To start things off, since you have an array of strings, not of characters, you have to declare it as char* array[19];

Then, you can declare the following (always useful) macro

#define ARR_SIZE(arr) ( sizeof((arr)) / sizeof((arr[0])) )

Last, you can choose arr[rand() % ARR_SIZE(arr)] (while keeping in mind that performing % on rand() is not the proper way to do get a random number within a range.

answered Jun 20, 2013 at 13:51
Sign up to request clarification or add additional context in comments.

2 Comments

On most systems, you will get the same answer each time you run the program unless you seed the random number generator with a different value each time. Check out srand(), but be aware that getting a decent seed is actually rather hard. And the drand48() family of PRNG functions is quite useful too, though you still have the seeding problem.
That is a correct comment, however I was referring to the issue of selecting a random number within a range of numbers in such a manner as to remain evenly distributed.
3
int n = rand()%20;
printf("%s\n", array[n]);
answered Jun 20, 2013 at 13:49

Comments

2

You can try array[rand() % ARRAY_LEN] but you are going to get a single char and not a char*

and when you are doing array[0] = "Hi"; it's not correct since you are assigning to a single char a char*

or turn your char array[20] into a char *array[20] and you can assign a string of characters

answered Jun 20, 2013 at 13:49

1 Comment

You are correct. I was getting seg fault before I modify the array into a pointer. (Upvote, thanks)
0

What you propose is the best solution there is - choose a random index and then use the element at this index. If your question is how to get a random integer, use the built-in function rand().

answered Jun 20, 2013 at 13:46

Comments

0

This can be done using rand in the c library stdlib.h

You can get a random number like this:

char random_elem = array[rand()%20];

and you can print it out like this:

printf("%d",array[rand()%20]);

answered Jun 17, 2019 at 16:21

1 Comment

Thank you for your contribution, but this question already has few good-quality answers and your answer adds nothing new. How about focusing on some unanswered C questions?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.