Mabadai
Mabadai

Reputation: 355

Random numbers double type

I had an exercise in my book which made me to puzzle the answer by a long series of attempts which took me hours to configure out. I'm new to c++, and programming at all, which makes me lack of knowledge(actually I'm learning at home by the book). So i got a couple of questions which i hope you could answer them and "asset" my brains on the right place.

Before the questions, the exercise was :

"Write a random-number generator that returns a random floating point number between 0.0 and 1.0. (Hint: Call rand, cast the result r to type double by using static_cast(r), and then divide by the highest value in the int range, RAND_MAX.) Make sure you declare the function with the double return type.".

The questions:

  1. what is the main function/idea that stands behind the srand(time(nullptr))? I keep getting a warning of c4244 ('argument': conversion from 'time_t' to 'unsigned int'). I was searching the answer and I figured out the problem is in the srand command... please somebody can explain it?

  2. I'm not truly clear about the static_cast and all the other 3 casting types . I know that in the static_cast i can define a new enum type from the parentheses to the braces from the left: static_cast<double>(r). But, what is the limit/definition of each cast(dynamic_cast, reinterpret_cast and const_cast)? When do i use them?

  3. I figured out that i must declare a double before the (RAND_MAX) statement, but i have no idea why it must be double and not int or float? What is the main idea that stands behind the RAND_MAX?

This is the code that i wrote(note: I'm using #include "stdafx.h" pre header just in case of... while in the book there is nothing mentioned about it):

#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

double rand_(int r);

int main() {
    int n;
    double r;

    cout << " choose a number: ";
    cin >> n;

    srand(time(nullptr));

    for (int i = 0; i <= n; i++) {
        r = rand_(n) ;
        cout << r << " ";
    }

    return 0;
}

double rand_(int r) {
    static_cast<double>(r);

    return rand() / double(RAND_MAX);

}

If you notice any "clearance" mistakes of basic coding please correct me.

Thanks for all who answer and support this post. Hope in future could do the same.

Upvotes: 0

Views: 2491

Answers (1)

Andrew Kashpur
Andrew Kashpur

Reputation: 746

srand(time(nullptr));

its used to initialise pseudo random number generator, with seed, obtained from current time (to get different rundom numbers each time you run your ptogramm). srand() accepts unisgned int parameter, time() return time_t, so here is your warning about conversion.

rand() / double(RAND_MAX)

rand() return integer value, RAND_MAX is integer constant, so to get floating point division, instead of integer division RAND_MAX is conversed to floating point type.

As for when to use which type of cast its bit to broad, and I'm pretty sure you can find info about every particular cast on SO or cppreference.

Upvotes: 1

Related Questions