Click here to Skip to main content
16,011,120 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionFunction for random number 'between limits' - how? Pin
James Millson22-Jun-01 8:26
James Millson22-Jun-01 8:26 
AnswerRe: Function for random number 'between limits' - how? Pin
Ben Burnett22-Jun-01 9:10
Ben Burnett22-Jun-01 9:10 
GeneralRe: Function for random number 'between limits' - how? Pin
Tomasz Sowinski22-Jun-01 9:28
Tomasz Sowinski22-Jun-01 9:28 
GeneralRe: Function for random number 'between limits' - how? Pin
Ben Burnett22-Jun-01 10:04
Ben Burnett22-Jun-01 10:04 
GeneralRe: Function for random number 'between limits' - how? Pin
Tomasz Sowinski22-Jun-01 10:24
Tomasz Sowinski22-Jun-01 10:24 
AnswerRe: Function for random number 'between limits' - how? Pin
22-Jun-01 12:46
suss22-Jun-01 12:46 
GeneralRe: Function for random number 'between limits' - how? Pin
Malcolm McMahon24-Jun-01 22:02
Malcolm McMahon24-Jun-01 22:02 
AnswerRe: Function for random number 'between limits' - how? Pin
Tim Deveaux22-Jun-01 14:09
Tim Deveaux22-Jun-01 14:09 
Ok, here's my try Smile | :)

I decided to go the mundane inelegant math route (though I bet there are elegant ways to do this). This code computes a number in the range nLower to nUpper, and adds it to nLower. One major stumble is that you need to be able to store the range, so an int wont cut it.

Also, this code has a problem, in that it returns numbers from nLower to nUpper-1. You can extend the range by 1 in most cases, but you run into snarky special case code. I'll leave it like this - season to taste.

int GetRandomNumberBetween(int nLower, int nUpper){    
 
    int              nTheRandomNumber = 0;
    unsigned int     range, offset;
    float            r;
 
    // assume nothing! (or do a min() max() thing)
    assert( nUpper >= nLower );
 
    // important that range be unsigned
    range = (nUpper - nLower);
 
    // RAND_MAX may be smaller than int32 max, but the point is
    // only to get a number between 0 and 1
    r = (float)rand() / (float)RAND_MAX;
 
    // offset may be 0 if both inputs are the same - which is ok
    offset = (int)(r * range);
 
    nTheRandomNumber = nLower + offset;

    TRACE(_T("nTheRandomNumber = %d\n"), nTheRandomNumber);
    return nTheRandomNumber;
}

AnswerRe: Function for random number 'between limits' - how? (Revisited) Pin
Tim Deveaux23-Jun-01 5:05
Tim Deveaux23-Jun-01 5:05 
AnswerRe: Function for random number 'between limits' - how? Pin
Anders Molin23-Jun-01 11:35
professionalAnders Molin23-Jun-01 11:35 
GeneralRe: Function for random number 'between limits' - how? Pin
Tim Deveaux23-Jun-01 13:28
Tim Deveaux23-Jun-01 13:28 
GeneralRe: Function for random number 'between limits' - how? Pin
Anders Molin24-Jun-01 1:40
professionalAnders Molin24-Jun-01 1:40 
AnswerRe: Function for random number 'between limits' - how? Pin
PJ Arends24-Jun-01 11:56
professionalPJ Arends24-Jun-01 11:56 
Generalphilosophy of error handling Pin
Amit Jain22-Jun-01 8:02
Amit Jain22-Jun-01 8:02 
GeneralRe: philosophy of error handling Pin
Tim Deveaux22-Jun-01 14:37
Tim Deveaux22-Jun-01 14:37 
Generalthanks =) Pin
Amit Jain23-Jun-01 19:18
Amit Jain23-Jun-01 19:18 
QuestionHow can i use the UDP on the web ?? Pin
khamis22-Jun-01 7:53
khamis22-Jun-01 7:53 
AnswerRe: How can i use the UDP on the web ?? Pin
markkuk24-Jun-01 20:59
markkuk24-Jun-01 20:59 
GeneralRe: How can i use the UDP on the web ?? Pin
khamis25-Jun-01 2:45
khamis25-Jun-01 2:45 
GeneralWindows NT4 and HP-950c Printer Pin
#realJSOP22-Jun-01 6:21
professional#realJSOP22-Jun-01 6:21 
GeneralComparing Strings. Pin
John Uhlenbrock22-Jun-01 5:51
John Uhlenbrock22-Jun-01 5:51 
GeneralRe: Comparing Strings. Pin
Jim Howard22-Jun-01 6:02
Jim Howard22-Jun-01 6:02 
GeneralRe: Comparing Strings. Pin
John Uhlenbrock22-Jun-01 6:41
John Uhlenbrock22-Jun-01 6:41 
GeneralRe: Comparing Strings. Pin
Jim Howard22-Jun-01 7:08
Jim Howard22-Jun-01 7:08 
GeneralRe: Comparing Strings. Pin
Tomasz Sowinski22-Jun-01 7:18
Tomasz Sowinski22-Jun-01 7:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.