41386

random element from array in c

Question:

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.

Answer1:

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 <strong>not</strong> the proper way to do get a random number within a range.

Answer2:

int n = rand()%20; printf("%s\n", array[n]);

Answer3:

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

Answer4:

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().

Recommend

  • Writing Merge Sort Pseudo-Code Procedure in C
  • how to know the last row filled in vba (excel)? [duplicate]
  • OSStatus error -50 (invalid parameters) AudioQueueNewInput recording audio on iOS
  • Runtime error in UVA Online Judge [closed]
  • How to work with AMMediaType for video filters
  • mapping between two ontologies
  • custom string delimiters stringtemplate-4
  • How to search a CSV file with php by checking if a date falls between 2 ranges
  • incomplete type 'struct' error in C
  • JSON - slashes not escaping
  • VBA Dir function not working on Excel 2010
  • Swift Initialization Rule Confusion
  • Inversing an interpolation of rotation
  • Need code translation from VB to C#
  • Best practice to eliminate magic numbers within a member function
  • several dataProvider per one Test in TestNG
  • How to unpack 32bit integer packed in a QByteArray?
  • Validate jQuery plugin, field not required
  • KnockoutObservableArray with typed elements in TypeScript
  • How to specify input and output paths from cmd.exe for a PowerShell script?
  • How to make JSON.NET deserialize to Microsoft Date Time?
  • Checking if an array in C is symmetric
  • Eliminate partial duplicate rows from result set
  • How does this usort cmp function actually work?
  • What does 'Language neutral' mean with regard to MAKELANGID?
  • C: Incompatible pointer type initializing
  • Sencha Touch 2.0 Controller refs attribute not working?
  • When to use `image` and when to use `Matrix` in Emgu CV?
  • Why is the size of this struct 32?
  • What is the “return” in scheme?
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • When should I choose bucket sort over other sorting algorithms?
  • How to make Safari send if-modified-since header?
  • How to pass list parameters for each object using Spring MVC?
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • python regex in pyparsing
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Android Google Maps API OnLocationChanged only called once
  • JaxB to read class hierarchy
  • How can I use threading to 'tick' a timer to be accessed by other threads?