0

I've pieced together a small program designed to build random names. It is not finished, however it is currently repeating the same name if I attempt to re-run the program. Program is built on Ubuntu Linux, C++ running the emacs text editor.

Here is the full code below, I am still fairly new to C++ but haven't encountered this issue before.

#include <iostream>
#include <vector>
#include <iomanip>
#include <stdlib.h>

using namespace std;


string randomName(int length) {

  char consonents[] = {'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','z'};
  char vowels[] = {'a','e','i','o','u','y'};

  string name = "";

  int random = rand() % 2;
  int count = 0;

  for(int i = 0; i < length; i++) {

    if(random < 2 && count < 2) {
      name = name + consonents[rand() % 19];
      count++;
    }
    else {
      name = name + vowels[rand() % 5];
      count = 0;
    }

    random = rand() % 2;

  }

  return name;

}


int main() {

  cout << "Enter a name length: " << endl;

  int length;

  cin >> length;

  cout << randomName(length) << endl;

  return 0;

}

I have read the predictability for rand() and in the other questions that were similar to this they mention that rand() often repeats values. Surely this can't be every single time the program is run though can it?

In example: If I run the program with a name length of 5 I may get something like: tfuvi

If I then run the program a second time with a name length of 2 I will get: tf

If I then run the program a third time with the name length of 4 I will get: tfuv

This can't be coincidence right?

Any help is greatly appreciated, if there is any documentation that explains this or if anyone knows a reason please let me know, thanks.

Vescoci
  • 35
  • 1
  • 1
  • 5
  • See `srand` (https://linux.die.net/man/3/rand), and note that you can call it with the return val of `time` (https://linux.die.net/man/2/time) – lockcmpxchg8b Mar 17 '18 at 01:46
  • As a side note, you want `vowels[rand() % 6]`. – DYZ Mar 17 '18 at 01:47
  • 1
    And rather than using a hardcoded value such as 6, use `sizeof(vowels)`, i.e. `vowels[rand()%sizeof(vowels)]` – mvds Mar 17 '18 at 01:52
  • Gotcha, thank you all! Dyz, I thought that using % 6 would produce an out of bounds exception since the arraySize is 6 but the values are from 0 - 5 – Vescoci Mar 17 '18 at 02:02

3 Answers3

2

If no seed value is provided, the rand() function is automatically seeded with a value of 1.

Meaning that every time you run your program, you always get the same pseudo-random sequence. So, please seed your random number generator with srand(time(NULL)) or in some other way: Recommended way to initialize srand?

DYZ
  • 55,249
  • 10
  • 64
  • 93
0

Set up the seed before calling rand():

    srand(time(NULL));

Then you can call rand() % n

Also be sure to include ctime for time();

Maestro
  • 2,512
  • 9
  • 24
0

you must initialize the random seed, call this before use rand() function in your program

srand (time(NULL));
Jonnathan Q
  • 312
  • 1
  • 8