# any function like rand() to ger a randome number of type float?

Printable View

• 05-04-2002
ema
any function like rand() to ger a randome number of type float?
hi all

anyone knows whether there is a function like rand() but generates numbers of type float or not?

thanx :)
• 05-04-2002
jdinger
I don't know of one, but you could write a little function to take the return value from rand() and convert it for you with a simple multiplication and a typecast.

Code:

```int i=rand()%100; double d=(double)i/10;```
• 05-04-2002
Xei
Yeah
Thats what I would do as well. If you need a decimal there is really no differance than randomizing a whole number then just making it smaller.
• 05-04-2002
Prelude
Code:

```double drand ( void ) {   return ( (double)rand() / (double)RAND_MAX ); }```
-Prelude
• 05-04-2002
Prodigy
What the hell ill do ur homework for u

code:

#include <iostream.h>
#include <stdlib.h>
#include <conio.h>

int main ()
{

int number = rand() % 2;

while(!kbhit())
{
for(int y = 0; y < 8; y++)
{
for(int x = 0; x < 8; x++)
{
number = rand() %10;
cout<<number;
}
cout<<" ";
}
cout<<" ";
for(int z = 0; z < 4; z++)
{
number = rand() % 2;
cout<<number;
}
cout<<endl;
}

return 0;
}

There ya go
• 05-04-2002
Prodigy
HEHE