Hey, I've made a genetic algorithm program that's not generating very random numbers. Every time I run it, it generates the same ten million or so random numbers. The -exact- same set of ten million "random" numbers.
I'm using rand() bit with one srand( (unsigned)time(NULL) ) at the start of the program. The numbers look fairly random, and they're all different, it's just that I get the same set over and over even when I restart the program.
Is there an easy fix to this one? If not, I may just end up rewriting the code in MatLab to side-step the issue.
You should have a function for random number
It will look something like that:
int random_integer(int min,int max)
{int random_integer;
random_integer = rand()%(max-min)+1;}
then inside program you can write for loop for as many numbers as u want to output
for (int i=0;i<n;i++) // n -number of random numbers you want
{random_integer(1,10);
cout<<endl<<random_integer;}
-------------------------------------------
Your thing will look like this:
#include <iostream>
#include <ctime>
using namespace std;
int random_integer(int min,int max)
{int random_integer;
random_integer = rand()%(max-min)+min;}
main()
{
int n,r;
srand(time(0));
cout<<"How Many Random Numbers?";
cin>>n;
for (int i=0;i<n;i++) // n -number of random numbers you want
{
r=random_integer(1,10);
cout<<endl<<r;}