Urler
Urler

Reputation: 520

How to create array of random strings in C?

I tried creating an array of ten random strings that would print directions randomly. Such as first time "up down right ... rot_x" and second time "forward rot_y up ... down" etc. I tried using a char* pc and allocating memory for it with memset but that didn't work so I tried the following code but I'm getting weird output. How can I fix this?

int main()
{
  int r_num;
  char r_arr[10][10];

  for (int i = 0; i < 10; i++)
  {
    r_num = rand() % 10;

    switch(r_num)
    {
      case 0: strcpy(r_arr[0], "up");
              break;
      case 1: strcpy(r_arr[1], "down");
              break;
      case 2: strcpy(r_arr[2], "left");
              break;
      case 3: strcpy(r_arr[3], "right");
              break;
      case 4: strcpy(r_arr[4], "rot_x");
              break;
      case 5: strcpy(r_arr[5], "rot_y");
              break;
      case 6: strcpy(r_arr[6], "rot_z");
              break;
      case 7: strcpy(r_arr[7], "forward");
              break;
      case 8: strcpy(r_arr[8], "back");
              break;
      case 9: strcpy(r_arr[9], "reset");
              break;
      default:
        fprintf(stderr, "Cannot process input/n");
    }
  }

  for (int i = 0; i < 10; i++)
  {
    printf("%s ", r_arr[i]);
  }

  return 0;

}

here's my output:

up ?V? left right rot_x ?V?  forward back reset

Upvotes: 0

Views: 888

Answers (6)

Alan Omar
Alan Omar

Reputation: 4217

by looking at your output i noticed some strange values like ?v?, the problem is that not all numbers between 0 and 9 will be generated by the rand() function which mean that the corresponding array element(to those numbers) will not be initialized and therefor it contain garbage values from what ever was stored in that memory address.
i hope that explain why you are getting those strange values.

Upvotes: 0

Paul
Paul

Reputation: 331

As noted by others above, your issue is that you're not indexing your array with the iteration number of your loop. You had:

case 0: strcpy(r_arr[0], "up");

Whereas you should have had:

case 0: strcpy(r_arr[i], "up");

The additional thing that I wanted to point out is that rand() uses a linear equation (at least on many systems) so it will be impossible for you to ever get two even numbers in a row or two odd numbers in a row, which is not very random. Hence I suggest something like:

r_num = (rand() >> 8) % 10;

Upvotes: 1

hhafez
hhafez

Reputation: 39750

A few problems with your code are:

  1. You aren't seeding rand(), so every run of your program will generate identical output. You need to use srand() first with a seed. Traditionally one uses time().
  2. Secondly despite the randomness you are unrandomly (is that a word?) filling r_arr. "up" will always be first, "down" will always be second etc.... Instead you should do something like

    for (int = 0; i< 10; i++) {
       r_num = rand() % 10;
       strcpy(r_arr[i], getDirection(r_num));
    }
    

where getDirection() will return a string based on an integer input (e.g.: via a case statement that associates 0 with "up").

  1. Your r_arr needs to be initialized. There is no guarantee in your current code that each entry in the array will be populated with chars before being accessed. If you implement suggestion 2 then you wont have a problem. Today however your code is accessing potentially uninitialized memory.

Upvotes: 1

here is a code that fits exactly to your need :

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

int main()
{
  // we use this instruction to get a random result each time we run the program 
    srand(time(NULL));
    int r_num;
    char r_arr[10][10];
// the dictionary array will be used to take from it the possible strings 
    char dictionary[10][10]={"up","down","left","right","rot_x","rot_x","rot_x","forward","back","reset"};
    int i=0;
    for(i=0; i<10; i++)
    {
      // r_num will be assigned randomly an index from dictionary tab
      // the general equation is (rand()%(max-min+1))+min to get a random value 
      // between max and min inclusive
        r_num=(rand()%(9-0+1))+0;
      // we will put the random string chosen in the array r_num each time 
        strcpy(r_arr[i],dictionary[r_num]);
    }
     // this loop will print the result 
    for(i=0; i<10; i++)
    {
        printf("r_arr[%d]=%s \n",i,r_arr[i]);
    }

    return 0;
}

Upvotes: 0

4386427
4386427

Reputation: 44246

Print the strings inside the switch instead of the for-loop at the end.

Maybe you'll also need something like:

srand (time(NULL));

Upvotes: 0

Catherine MacInnes
Catherine MacInnes

Reputation: 191

As the commenters pointed out, you are randomizing not what value you put in each position but which positions get filled with their preset value. Also, your use of a switch statement here is just odd. Try something like:

char value_arr[10][10]={"up", "down", "left", "right", "rot_x", "rot_y", "rot_z", "forward", "back", "reset"}

 for (int i = 0; i < 10; i++)
  {
    r_num = rand() % 10;
    strcpy(r_arr[i], value_arr[r_num]);
  }

Upvotes: 0

Related Questions