Thread: random function

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    random function

    Hello..

    I want to have a random function so I could pass the next values in that way:

    random(1,7) -> would return a random number in range 1-7 (i used boost for this)
    random('A', 'Z') -> would return random letter in range A-Z (ABCDEF..)

    but I have no idea what would be the right approach to return random letter in some range?

    Would it be hard to do something like:
    random(1, 'Z') -> random number or capital letter

    Thanks for help and advices

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    A letter is just a one-byte integer.
    If this works:
    Code:
    random(1,7);
    then this should also work:
    Code:
    random('A','Z');
    You don't need to make any special things for that.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >You don't need to make any special things for that.
    You do if you want your code to be portable.

    >but I have no idea what would be the right approach to return random letter in some range?
    Create an array with your range and use a random index into that array:
    Code:
    // Random number [1, 7)
    int r1 = random ( 1, 7 );
    
    // Random letter
    const string letters ( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
    char r2 = letters[random ( 0, letters.size() )];
    >Would it be hard to do something like:
    >random(1, 'Z') -> random number or capital letter
    Not really, but it would depend on the exact behavior you want.
    My best code is written with the delete key.

  4. #4
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    As Prelude says, don't assume that the codes assigned to letters are in a contiguous sequence if you want your code to be portable.

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Code:
    random('A','Z');
    equals to
    Code:
    random(65,90);
    as
    Code:
    RegisterHotKey(hwnd,1,MOD_ALT,65);
    equals to
    Code:
    RegisterHotKey(hwnd,1,MOD_ALT,'A');
    What do you mean by contiguous sequence?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That is only true for the ASCII character set. There are other character sets out there.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >What do you mean by contiguous sequence?
    C++ guarantees that the characters '0,'1','2','3','4','5','6','7','8','9' are in that order, with nothing inbetween. No such guarantee exists for any other characters. In other words, somewhere between 'A' and 'Z' could be something not even remotely resembling a letter.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    630
    Code:
    int r1 = random ( 1, 7 );
    
    // Random letter
    const string letters ( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
    char r2 = letters[random ( 0, letters.size() )];
    Shouldnt there be letters.size() - 1?

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Shouldnt there be letters.size() - 1?
    It depends on how random is defined. I was assuming that it's the usual half open range that we all know and love. In that case, the random number returned would be never go below 0 and never go above 25, which is a perfectly valid index.
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    630
    I have a problems with my rand function... It will return the same value each time I call it with my program.. What am I doing wrong?

    btw, I use boost.

    Code:
    using namespace boost;
    
    int my_rand(int min, int max) {
    	mt19937 rng;
    	rng.seed( static_cast<unsigned> (time(0)) );
    
    	uniform_int<> dist(min, max);
    	variate_generator<mt19937&, uniform_int<> > die(rng, dist);
    
    	return die();
    }

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >What am I doing wrong?
    You only need to seed once. Or if the program is long running, very rarely.
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    May 2006
    Posts
    630
    So I have to make mt19937 global and seed from the main() function?

  13. #13
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    What is this supposed to be doing?

    Code:
    static_cast<unsigned>
    Just as a matter of interest.

  14. #14
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by l2u
    So I have to make mt19937 global and seed from the main() function?
    maybe enough to make it static inside the current function and initialize on the first call?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Quote Originally Posted by SKeane
    What is this supposed to be doing?

    Code:
    static_cast<unsigned>
    It's a C++ style cast. The equivalent C-style cast would be:
    Code:
    rng.seed ( (unsigned)time ( 0 ) );
    What it's supposed to be doing is converting the time_t value that time returns to the unsigned value that seed expects. What it's actually doing may not be what it's supposed to be doing because that conversion isn't guaranteed to work.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM