generate rand() in text box

This is a discussion on generate rand() in text box within the C++ Programming forums, part of the General Programming Boards category; Hi , Trying to generate 6 digit random number and display in text box as RN: XXXXXX where X is ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2

    generate rand() in text box

    Hi ,

    Trying to generate 6 digit random number and display in text box as RN: XXXXXX where X is the random number, have had some help but still unable to do the following:

    1. Adding the "RN:" parameter to SetDlgItemInt function.
    2. Converting iRandomNo to a displayable format.

    Please advise , or point to example snippits.

    thnx (hope the format is OK)

    Code:
    HWND hcombo = GetDlgItem(  hwnd, IDC_TEXTBOX_EDIT);  
    			  
            int iRandomNo=0; 
    
            for(int i=0;i<6;i++) 
    
       {  
           iRandomNo = iRandomNo*10 + rand()/10;
       } 
    
           std::ostringstream stm;
    
    
    SetDlgItemInt(hwnd, IDC_TEXTBOX_EDIT, "RN:", stm);

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    rand()%10;
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Trying to generate 6 digit random number and display in text box as RN: XXXXXX where X is the random number
    Code:
    HWND hcombo = GetDlgItem(  hwnd, IDC_TEXTBOX_EDIT);  
    			  
       int iRandomNo=0; 
    
       for(int i=0;i<6;i++) 
    
       {  
           iRandomNo = iRandomNo*10 + rand()/10;
       } 
    
       std::ostringstream stm;
       stm << "RN:" << iRandomNo;
    
       SetDlgItemText(hwnd, IDC_TEXTBOX_EDIT, stm.str().c_str());

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    > rand()/10
    See vart's post.

    Actually, since RAND_MAX is at least INT_MAX which is at least 2^16-1 (65535), you could generate two three digit numbers, or one five digit and one one digit number.
    Code:
    iRandomNo = ( (rand() % 1000) * 1000) + (rand() % 1000);
    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.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >See vart's post.
    That's not a good way. And my post was addressing the dialog box question. Admittedly I never looked at the random number part. I just copied it verbatim.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    It's better than rand()/10!
    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.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Ok, I'm sorry for the confusion. Here's a correction to the rand() code I posted above:
    Code:
           iRandomNo = iRandomNo*10 + (int)(((double) rand() * 10.)/(RAND_MAX + 1.));
    Last edited by swoopy; 12-06-2006 at 01:29 PM.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Right, sorry.

    Though I still think that generating 2 numbers would be a good idea.
    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.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> Actually, since RAND_MAX is at least INT_MAX.
    Why do you say that? I thought I remember seeing a rand_max of 32767 or so on a machine with a 4 byte int.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Though I still think that generating 2 numbers would be a good idea.
    True, I believe that's the best way of doing it. And I missed the bug in the random number generation formula. I knew something didn't look quite right. But I was focusing on questions 1. and 2. in the original post.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Obtaining a value from a text box
    By thetinman in forum Windows Programming
    Replies: 1
    Last Post: 11-23-2006, 04:50 PM
  2. Automatically enter text in a dialog box
    By leojose in forum Windows Programming
    Replies: 6
    Last Post: 12-13-2005, 10:59 AM
  3. Dynamically add statis text to a dialog box
    By earth_angel in forum Windows Programming
    Replies: 8
    Last Post: 06-23-2005, 01:28 PM
  4. Sending text to an edit box
    By ColdFire in forum Windows Programming
    Replies: 1
    Last Post: 09-24-2002, 07:46 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

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