Q:

Random number between zero and one in c++

i want to create a random number between 0 and 1 but my codes output is zero.
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
  srand(time(0));

  for(int x=0;x<100;x++)
  {
      cout<<(rand()%10)/10<<endl;
  }
  return 0;
}

A:

This: (rand()%10)/10 gives you a (low quality) random integer between zero and RAND_MAX, then you use modulo to turn that into a (biased) integer between zero and nine, then you perform integer division by 10 which will always be a value less than 1 which will be truncated to zero.
A much better solution would be to use the random facilities from C++11 and use a std::uniform_real_distribution to obtain your value between 0 and 1.

