0

First off this could be complete rubbish as I am new to C++ classes and have never used a random number generator before. But this is my attempt. It generates a random number between the values that I want, thats fine. but when outputting them via an array, all the random values are the same.

RandomNumberGenerator.h

#pragma once
#include <iostream>
#include <cstdlib>
#include <ctime>


class RandomNumber
{
public:
void randomNumber();
int actualRandomNumber;
};

RandomNumberGenerator.cpp

#include "RandomNumberGenerator.h"

void RandomNumber::randomNumber()
{
srand (time(NULL));

actualRandomNumber = rand() % 66 + 1;
}

Game.h

#include "RandomNumberGenerator.h"

class Game
{
private:
int myNumbers[6];


public:
void createGame();
};

Game.cpp

#include "Game.h"

void Game::createGame()
{
RandomNumber create;

std::cout << "Welcome to your game!" << std::endl;
for (int i = 0; i < 6; i++)
{
create.randomNumber();
myNumbers[i] = create.actualRandomNumber;   
}

for (int i = 0; i < 6; i++)
{
std::cout << myNumbers[i] << std::endl;
}
}

main

#include "Game.h"
#include "RandomNumberGenerator.h"

int main()
{
Game play;

play.createGame();

system("pause");
return 0;
}

Thanks in advance for anyones time.

user3001499
  • 811
  • 4
  • 16
  • 32
  • 3
    **DON'T** call `srand()` multiple times. Call it once at the beginning, then leave it alone and just call `rand()`. Or use C++11 random number generators. – Cornstalks Nov 17 '13 at 17:09
  • sorry if this is thick of me, but haven't I only called it once - in RandomNumberGenerator.cpp – user3001499 Nov 17 '13 at 17:14
  • @user3001499 no you call it each time `randomNumber` is called – Ivaylo Strandjev Nov 17 '13 at 17:15
  • 2
    Read why srand should only be called once in the linked duplicate. And afterwards, watch this: [Youtube: rand() Considered Harmful](https://www.youtube.com/watch?v=LDPMpc-ENqY) – leemes Nov 17 '13 at 17:15
  • @Cornstalks - however, seeding C++11 random number generators presents the same issue as seeding `rand`. – Pete Becker Nov 17 '13 at 17:20
  • @PeteBecker well, you should seed them with a `random_device`; the seed is then non-pseudo random (at least that's the idea). But of course it's still a bad idea to always seed again. – leemes Nov 17 '13 at 17:24
  • @leemes - maybe, but to the extent that that's important, it's important for `rand` **and** for the C++11 generators. There simply is no essential difference here. The point being that "use C++11 random number generators" does not magically make seeding into something you don't have to think about. – Pete Becker Nov 17 '13 at 17:27
  • @PeteBecker: indeed, but typically you seed when you create the generator, and when I said that I wasn't thinking he would constantly create and destroy the random generator object (but of course, that probably wasn't a good assumption). – Cornstalks Nov 17 '13 at 21:14
  • @Cornstalks - I'm not following. My statement, paraphrased, was simply that you typically seed once, regardless of whether you're using `rand` or the C++11 generators. – Pete Becker Nov 17 '13 at 21:49

2 Answers2

3

Everytime you call srand(time(NULL)), you set the starting point of your sequence depending on a value that only changes once a second, thus your number only changes once a second (independend from number of calls).

So only calling srand() once will fix your code.

Notice, that both rand() and your actualRandomNumber = rand() % 66 + 1; are really bad regarding their "randomness" (why). Use the C++ STL instead, consider the example on the bottom of the page (you want the uniform int distribution).

Edit: Typo and link to OneC++ Talk

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
2

The pseudo random number generator starts with a certain number and then generates a sequence based on the first number.

When the first number is the same the output sequence will be the same.

To generate different sequences each time you launch your program, the idea is to use the starting time of your program relatively to a specific date in miliseconds.

So the Error in your code is that you placed the srand(time(NULL)) in the function randomnumber() and it's being called in the loop. Because the CPU is so fast the time(NULL) (the first number in the sequence) will return the same value in miliseconds. Thus your having the same sequence. To solve this place srand(time(NULL)) in main()

Paul Draper
  • 78,542
  • 46
  • 206
  • 285
ELKA
  • 735
  • 6
  • 17
  • thanks - took your advice and stuck srand.... in main(). Thanks everyone else for the advice/information about everything else – user3001499 Nov 17 '13 at 17:29