Random Numbers

This is a discussion on Random Numbers within the C++ Programming forums, part of the General Programming Boards category; I just got done reading this about how to correctly use rand() and time(). I notice that I compiler warnings ...

  1. #1
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391

    Random Numbers

    I just got done reading this about how to correctly use rand() and time(). I notice that I compiler warnings about converting to int from double.

    So to resolve the warnings I've explicitly cast my return statement in these functions:
    Code:
    int get_rand( int UPPER_BOUND ){
    	
    	return (int)( uniform_deviate( rand() ) * UPPER_BOUND );
    }
    
    
    int get_rand( int LOWER_BOUND, int UPPER_BOUND ){
    	
    	return (int)( LOWER_BOUND + uniform_deviate( rand() ) * ( UPPER_BOUND - LOWER_BOUND ) );
    }
    Is this the correct way to cast the returns or should i use <static_cast>? Or is there another more correct solution?
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,452
    It is correct, but static_cast would be better.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    For C++, it's better to use static_cast, yes... It's preferable to avoid casts and foremost, C-style casts.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Great link! I'll be busy reading through for a while. Mucho gracias!
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 09:47 PM
  3. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 06:51 PM
  4. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM
  5. random numbers
    By lil_plukyduck in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2003, 09:14 PM

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