Generate Random Number

This is a discussion on Generate Random Number within the C++ Programming forums, part of the General Programming Boards category; Hi everybody Code: int intrnd () { double const a = 16807; /* ie 7**5 */ double const m = ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    85

    Talking Generate Random Number

    Hi everybody

    Code:
    int intrnd ()
    {
      double const a    = 16807;      /* ie 7**5 */
      double const m    = 2147483647; /* ie 2**31-1 */
      double temp = seed * a;
      seed = (int) (temp - m * floor ( temp / m ));
      return seed;
    }
    
    double rand_0to1()
    { return ( intrnd() / 2147483647.0 ); }
    
    double random_number()
    {
      double r;
      while ((r = rand_0to1() ) >= 1.0 ) ; /* ensure we don't return 1.0 */
      return ( r );
    }

    Any1 have idea how can i minimise the code? like 3-4 line instead all these???

    Thanks Thanks

  2. #2
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    To get random numbers (truly random and uniformly distributed over a given range) you'll probably need more than 3-4 lines of code...

    Use a seeding function that hashes the output of time like this:
    Code:
    unsigned time_seed( time_t* T ){
    	
    	time_t now = time( T );
    	unsigned char *p = ( unsigned char* )&now;
    	unsigned seed = 0;
    	size_t i;
     
    	for( i = 0; i < sizeof now; i++ )
    		seed = seed * ( UCHAR_MAX + 2U ) + p[i];
    
    	return seed;
    }
    then uniformly deviate your random pool over your range like this:
    Code:
    double uniformly_deviate( int seed ){
    	
    	static const double R_MAX = RAND_MAX + 1.0;
    	return seed * ( 1.0 / ( R_MAX ) );
    }
    and write a function to obtain your random number like so:
    Code:
    // For an int..
    int get_rand( ){
    	
    	static const double R_MAX = RAND_MAX + 1.0;
    	return static_cast<int>( uniformly_deviate( rand() ) * R_MAX );
    }
    
    
    
    // For a double...
    double get_rand_double( double LOWER_BOUND, double UPPER_BOUND ){
    	
    	return ( LOWER_BOUND + uniformly_deviate( rand() ) * ( UPPER_BOUND - LOWER_BOUND ) );
    }
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by peacealida View Post
    Hi everybody


    Any1 have idea how can i minimise the code? like 3-4 line instead all these???

    Thanks Thanks
    Here.

    Code:
     
    unsigned int RandomNumber(){
       unsigned __int64 hash;
       static unsiged int seed;
       
       if(seed == 0) seed = 1;
     
       hash = (seed * 0x77ed1ab3) &#37; 4294967291;   
       seed = hash; // ignore compiler warning
    
       return seed;
       }
    Last edited by abachler; 04-04-2008 at 01:24 PM.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    Quote Originally Posted by peacealida View Post
    Any1 have idea how can i minimise the code? like 3-4 line instead all these???
    Using the built-in rand and srand functions should cut it down a lot.

    Seriously, why write your own random functions that are worse than the built-in ones? I mean I can understand writing your own mersenne twister to improve upon the built-in one, but this...

    Try holding down the delete key for a bit
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    int intrnd () {
      double const a    = 16807;      /* ie 7**5 */
      double const m    = 2147483647; /* ie 2**31-1 */
      double temp = seed * a;  seed = (int) (temp - m * floor ( temp / m ));  return seed;}
    double rand_0to1()
    { return ( intrnd() / 2147483647.0 ); }
    double random_number() {  double r;  while ((r = rand_0to1() ) >= 1.0 ) ;  return ( r ); }
    Now that's several lines shorter

    As iMalc and dudeomandude hints, your random function is no better then the built in ones, and you could reduce it by several lines by using standard random numbers. Or you can write your own function that is better - but that would probably take a few more lines [at least if you don't want to mess up the formatting like I did - C is not a language where lines actually matter much - it's more what's on the lines that produce code that matters]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    unsigned int RandomNumber(){
       unsigned __int64 hash;
       static unsiged int seed;
       
       if(seed == 0) seed = 1;
     
       hash = (seed * 0x77ed1ab3) &#37; 4294967291;   
       seed = hash; // ignore compiler warning
    
       return seed;
       }
    Compiler warnings exist for good reason . . .
    Code:
       seed = (unsigned)hash;
    Besides the shortcomings already mentioned with your code, it's not very portable. I think __int64 is MS-specific.

    For a quick and easy way to generate random numbers: http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    For more detail about random numbers with better distribution: http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    85
    Hi
    Sorry had few attempt, buy code doesn't seems to work after simplify

    Any1 can help
    thanks

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by peacealida View Post
    Hi
    Sorry had few attempt, buy code doesn't seems to work after simplify

    Any1 can help
    thanks
    What code, and in what way does it not work?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    85
    Code:
    int intrnd () {
      double const a    = 16807;      /* ie 7**5 */
      double const m    = 2147483647; /* ie 2**31-1 */
      double temp = seed * a;  seed = (int) (temp - m * floor ( temp / m ));  return seed;}
    double rand_0to1()
    { return ( intrnd() / 2147483647.0 ); }
    double random_number() {  double r;  while ((r = rand_0to1() ) >= 1.0 ) ;  return ( r ); }

    I have tried these code, but somehow occur 23 error

    Thanks for the follow up and reply

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    I have tried these code, but somehow occur 23 error
    What are the errors? At a glance, it is the same as your original example, except that matsp was trying to prove that reducing the number of lines of code may not be all that important.
    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

  11. #11
    Registered User
    Join Date
    Mar 2008
    Posts
    85
    Sorry It is working i had a messsssssssssssssssssssssssssssssssssssssssssssss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do I generate a random number? (easily?)
    By fsx in forum C Programming
    Replies: 7
    Last Post: 05-12-2009, 05:04 AM
  2. rapid random number generation problem
    By Newton in forum C Programming
    Replies: 17
    Last Post: 09-19-2008, 02:08 PM
  3. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  4. random number in range
    By lgg in forum Linux Programming
    Replies: 3
    Last Post: 08-14-2005, 05:15 AM
  5. Replies: 2
    Last Post: 01-04-2004, 04:52 PM

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