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

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    11

    Red face 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

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    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;

  3. #3
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719

    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.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    double drand ( void )
    {
      return ( (double)rand() / (double)RAND_MAX );
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User Prodigy's Avatar
    Join Date
    May 2002
    Posts
    25
    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
    And the cows moo...
    AIM:ProdigyCpp
    E-Mail:[email protected]
    If you like my avatar plz dont copy it go to:
    http://www.dragid.com/

  6. #6
    Registered User Prodigy's Avatar
    Join Date
    May 2002
    Posts
    25
    HEHE
    And the cows moo...
    AIM:ProdigyCpp
    E-Mail:[email protected]
    If you like my avatar plz dont copy it go to:
    http://www.dragid.com/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  3. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  5. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM