random function

This is a discussion on random function within the C++ Programming forums, part of the General Programming Boards category; Hello.. I want to have a random function so I could pass the next values in that way: random(1,7) -> ...

  1. #1
    l2u
    l2u is offline
    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,796
    >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,319
    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,796
    >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
    l2u
    l2u is offline
    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,796
    >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
    l2u
    l2u is offline
    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,796
    >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
    l2u
    l2u is offline
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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.

Page 1 of 2 12 LastLast
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, 12:49 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01: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, 06:42 PM

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