Random number...

This is a discussion on Random number... within the C++ Programming forums, part of the General Programming Boards category; 11 C:\Dev-Cpp\Untitled1.cpp [Warning] assignment to `int' from `double' <code> #include <iostream.h> #include <stdlib.h> #include <time.h> int main() { int i; ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    9

    Random number...

    11 C:\Dev-Cpp\Untitled1.cpp
    [Warning] assignment to `int' from `double'

    <code>
    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>

    int main()

    {

    int i;

    i=rand() / (double)RAND_MAX * 12;

    int thetime=time(NULL);
    srand(thetime);

    cout<<i;

    return 0;

    }
    </code>

    What is wrong with that ?

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    To use code tags, you need to use square brackets not angles.. '[]' not '<>'.

    Next, you should 'seed' the random number generator before you use it, you are doing it the other way round. Finally, the warning you are getting is because you are assigning a floating point value to an integer. If you are only interested in the integer part of your random number, you can cast it to an int to tell the compiler that you know what you are doing. Note: compiler warnings are not the same as errors, however, ignore them at your peril.

    i = (int) (rand() / (double)RAND_MAX * 12);
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    9
    Originally posted by adrianxw
    To use code tags, you need to use square brackets not angles.. '[]' not '<>'.

    Next, you should 'seed' the random number generator before you use it, you are doing it the other way round. Finally, the warning you are getting is because you are assigning a floating point value to an integer. If you are only interested in the integer part of your random number, you can cast it to an int to tell the compiler that you know what you are doing. Note: compiler warnings are not the same as errors, however, ignore them at your peril.

    i = (int) (rand() / (double)RAND_MAX * 12);

    Even if i insert that line it doesnt return a random number :/ always returns zero.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Post your revised code.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    9
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    
    {
    
    int i;
    
    i = (int) (rand() / (double)RAND_MAX * 12);
    
    int thetime=time(NULL);
    srand(thetime);
    
    cout<<i;
    
    return 0;
    
    }

  6. #6
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    You want to seed the generator before using rand or the first call will always use the same seed. Also time() doesn't return an int, it returns a time_t. It usually helps to test your generator by looping over it a few times to make sure that the numbers change, try this :-)
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
      srand(time(NULL));
    
      for (int i = 0; i < 10; i++)
      {
        int r = (int) (rand() / (double)RAND_MAX * 12);
        cout<<r<<endl;
      }
    }
    *Cela*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rapid random number generation problem
    By Newton in forum C Programming
    Replies: 17
    Last Post: 09-19-2008, 03:08 PM
  2. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 10:46 AM
  3. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 01:56 PM
  4. random number tutorial
    By 7stud in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2005, 03:41 PM
  5. Random Number Generator
    By Ikurik in forum C++ Programming
    Replies: 16
    Last Post: 08-17-2003, 08:34 PM

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