-1

I'm probably being blind, nut I get the same results each time I run this console application, despite using random numbers. Can anybody kindly explain where I'm going wrong? Here is the code:

#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <stdio.h>

using namespace std;

bool bacteria(long mut, long chance){
        bool result;
    if (mut >= chance){
         result = true;
    }
    else{
        result = false;
    }
    return result;
}
int run = 1000000;//Number of iterations
int mutations;
int survival;

void domutation(){
    mutations = 0;
    survival = 0;
    for (int i = 0; i < run; i++){
        long x = rand() % 2;
        long y = rand() % 1000000;
        bool run = bacteria(x, y);
        if (run == true){
            mutations++;
        }
        else if (run == false) {
            survival++;
        }
    }
    cout << "Mutations: " << mutations << "   Survivals: " << survival << endl;
}

int main(){
    for (int x = 0; x < 10; x++){
        domutation();
    }
    int wait;
    cin >> wait;
}

Each individual iteration of domutation() produces a different result to the previous iteration, but each time I run the application, the results are always the same as they were the last time I ran it, e.g. the first iteration always produces 38 mutations, and the last always produces 52, and all in between are unchanged.

I'm sure I'm doing something dopey!

I am working in VS 2013 in Windows 8.1.

Thanks!

Guy Stimpson
  • 87
  • 11
  • *"despite using random numbers"* You're *not* using random numbers. You're using pseudo-random numbers. – dyp Jul 01 '14 at 20:55
  • 2
    You should use the C++11 `` header. – Baum mit Augen Jul 01 '14 at 20:56
  • your not seeding the random number generator – user1937198 Jul 01 '14 at 20:56
  • 1
    use srand(time(NULL)) as the first line of your main. You have the same seed every time you run the program. The same seed - the same generated numbers – grisha Jul 01 '14 at 20:56
  • Excellent, that does the trick, thank you. I knew it would be something blindingly obvious. I had a different (and so therefore incorrect) understanding of pseudo-random numbers. Thank you all. – Guy Stimpson Jul 01 '14 at 21:52

1 Answers1

1

rand gives you a predictable stream of numbers. You need to seed it to choose a different point in this stream to start. Assuming you won't run your program more than once per second, the current time is a cheap/easy seed.

int main(){
    srand(time(NULL));
    for (int x = 0; x < 10; x++){
        domutation();
    }

Note that not providing a seed is equivalent to always starting your program with srand(0)

simonc
  • 41,632
  • 12
  • 85
  • 103
  • Many thanks. I had an erroneous understanding of pseudo-random numbers. Thank you for this. What would I have to do if I wanted to run the simulation more than once per second? I don't but I'm curious... – Guy Stimpson Jul 01 '14 at 21:55
  • 1
    @GuyStimpson You'd need to find a seed that changed more than once per second. E.g. [gettimeofday](http://pubs.opengroup.org/onlinepubs/000095399/functions/gettimeofday.html) which provides microsecond resolution. – simonc Jul 01 '14 at 21:58