rand ( ) with decimal values

Hello!

I'm just need somebody to tell me if everything I have here its right and what its wrong if anything its wrong.

Prototype
1
2
//Para calcular numeros aleatoreos de numeros reales.
double random(float, float, int);


Call
1
2
3
4
5
//Para imprimir el resultado al lugar decimal deseado por el usuario.
cout << fixed << setprecision(num3);

//Devuelve el valor  ramdom de numeros reales.
cout << random(float val1, float val2, int val3);


Definition
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Para calcular numeros aleatoreos para numeros reales.
double random(float num1, float num2, int num3)
{
	//Para darle valor a result.
	double result;

    //Para darle valor entero a num1 y num2.
	int
		num1 = num1 * 100,
		num2 = num2 * 100;

	result = num1 + rand() % (num2 - num1 +1)

		static_cast <float> (result);

		return result;
}


Another thing, Do I must define the (int num3) listed on the Parameter list in the function of definition?, so it'll be useful in the invocation...

Thanks, Hope "ANYwhO" answers me :D
PSPMAN90
Last edited on
You don't need to specify the types when calling a function (usually).

I think you should divide result by 100 before returning it.

What's num3 for?
Ohh, I forgot! ha! I fix it!

1
2
3
4
5
6
7
8
9

//Devuelve el valor ramdom de numeros enteros.
cout << random( val1, val2);

//Para imprimir el resultado al lugar decimal deseado por el usuario.
cout << fixed << setprecision(num3);

//Devuelve el valor  ramdom de numeros reales.
cout << random( val1, val2, val3);



num 3 its the decimal place chosen by the user.
I used cout << fixed << setprecision (num3); on call
Its that ok? to put it on call and use a variable for the value of setprecision?


Definition
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

//Para calcular numeros aleatoreos para numeros enteros.
long random(int num1, int num2)
{
	return num1 + rand() % (num2 - num1 +1);
}

//Para calcular numeros aleatoreos para numeros reales.
double random(float num1, float num2, int num3)
{
	//Para darle valor a result.
	double result;

    //Para darle valor entero a num1 y num2.
	int
		num1 = num1 * 100,
		num2 = num2 * 100;

	result = num1 + rand() % (num2 - num1 +1)

		static_cast <float> (result/100);

	return result;
}



You don't think I should cast to double or its okay to cast to float even if the whole function is of type double?
Last edited on
Its that ok? to put it on call and use a variable for the value of setprecision?
Uh... Yeah... But the function is still not using num3. I don't know why you're passing it.

This does nothing: static_cast <float> (result/100); (I didn't notice it the first time.)
Last edited on
Yeah I know, because the function its double and result is double WHy am I casting it to float?!

Ok, so If I have it as part of the arguments listed in the Function I should define it as part of the function, but How I do that? If I won't need to use main here, I'm just need to show this 3 phases and its functions.

num3 its the value the user inputs as decimal place and it will show in call as
cout << fixed << setprecision(num3);



DO I need to declare result and num1 and num2 with those data types to use it inside the function? I mean num1 and num2 even if I declare them int inside the function still says its double...


I Changed the name of the variable inside the function for the data type to be used (int) and promotion its not used by the system.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Para calcular numeros aleatoreos para numeros reales.
double random(float num1, float num2, int num3)
{
	//Para darle valor a result.
	double result;

    //Para darle valor entero a num1 y num2.
	int
		numA = num1 * 100,
		numB = num2 * 100;

	result = (numA + rand() % (numB - numA +1))/100;


		return result;
}


Also the result variable should be double or it can be float too? Which one its more convenient?

Sorry for the load of Questions I just want to get all this clear.

Thanks

Last edited on
Ok, so If I have it as part of the arguments listed in the Function I should define it as part of the function, but How I do that?

num3 its the value the user inputs as decimal place and it will show in call as
cout << fixed << setprecision(num3);
You don't seem to be getting it.
You know what num3 does: it's passed to setprecision() from some function (I'm assuming main()). But, why would random() be interested in what the value of num3 is? It's obviously not using it for any formula, so why are you passing it?

DO I need to declare result and num1 and num2 with those data types to use it inside the function?
You mean like this?
1
2
3
4
double random(float num1, float num2, int num3){
    float num1, num2;
    //...
}

No, that won't work.
Ok, How about now , everything ok? No errors?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Para calcular numeros aleatoreos para numeros reales.
double random(float num1, float num2,)
{
	//Para darle valor a result.
	double result;

    //Para darle valor entero a num1 y num2.
	int
		numA = num1 * 100,
		numB = num2 * 100;
        
	    
	result = (numA + rand() % (numB - numA +1))/100;


		return result;
}


another point of view
--------------------------------------------------------------------------------------------------
Hey what about if I had to use the num3 inside that function, how would I use it regarding to set precision in the call? How would I define it to use it just for setprecision in call? It that would be possible and relevant or impossible and irrelevant?

Thanks man, I just want to figured this all and that's it, then I'll can sleep in calm

No rush by the way...

Thanks again
PSPMAN90
Last edited on
There's an extra comma in the signature.
result should be divided by 100. (Just do return result/100;)

Hey what about if I had to use the num3 inside that function, how would I use it regarding to set precision in the call? How would I define it to use it just for setprecision in call? It that would be possible and relevant or impossible and irrelevant?
I'm not sure I'm getting what you're asking. Are you asking whether you shouyld pass num3 to random() and let it call setprecision()? If that's the case, the answer is no. The function is called random(). All it should do is return a random number inside a range. It shouldn't concern itself with IO.
So it should never used for setprecision value to be set by the user...

That's why we got main and also we can make another function that does this decimal stuff right?

Thanks
Topic archived. No new replies allowed.