vendredi 11 octobre 2019

rand()/RAND_MAX function prints 1 every time

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;


void seedRandom()
{
  srand(time(NULL));
}

double nextRandom()
{
  seedRandom();
  double ran;
  ran = ( (double) rand()) / RAND_MAX;
  cout << ran;
  return ran;
}

int main()
{
    cout<<nextRandom;
}

Why does this print 1 every time and not a random number between 0 and 1? I looked everywhere but it does not seem to work. This is due at midnight. Also, this is only an excerpt from a long code.




Aucun commentaire:

Enregistrer un commentaire