Problem of heap memory in C++ after generating random number

-2

I have been struggling to run a genetic algorithm; however currently there is an issue when I want to generator random number. I use the one here. I have written it as follows:

#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>   
#include <algorithm>
#include <random>
#include <chrono>
#include <time.h>

double randomgens() {
    std::mt19937_64 rng;
    double currentRandomNumber;
    // initialize the random number generator with time-dependent seed
    uint64_t timeSeed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
    std::seed_seq ss{ uint32_t(timeSeed & 0xffffffff), uint32_t(timeSeed >> 32) };
    rng.seed(ss);
    // initialize a uniform distribution between 0 and 1
    std::uniform_real_distribution<double> unif(0, 1);
    // ready to generate random numbers
    const int nSimulations = 10;
    currentRandomNumber = unif(rng);
    return currentRandomNumber;
}

void main() {
    double random;
    random = randomgens();
    std::cout << "random number= " << random;
}

I call it in a double variable:

random = randomgens(); 

But when it gets to the randomgens function at the first I see the message visual studio triggered breakpoint and when I press continue I see an error:

I call it somewhere like:

Unhandled exception at 0x00007FFB6BB2AA2C (ntdll.dll) in project in Visual Studio.exe: 0xC0000374: A heap has been corrupted (parameters: 0x00007FFB6BB83180). 

Can anyone help me to sort out this problem?

Update1: I changed the way my random number were generated but problem still exists. This problem arises when I want to print random using cout. I dropped off the cout but still for other couts I see the same problem exists. Sometime when I run the program it works but while rerunning the error message pops up. What can I do?

c++
random
visual-studio-2015
heap
heap-memory
asked on Stack Overflow Oct 15, 2018 by questionaskerprogrammer • edited Oct 15, 2018 by Thomas Sablik

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0