Click here to Skip to main content
15,927,744 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
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 
AnswerRe: Function for random number 'between limits' - how? (Revisited) Pin
Tim Deveaux23-Jun-01 5:05
Tim Deveaux23-Jun-01 5:05 
I've gotten caught up in this - I think the code I posted above comes close, but I have the feeling that the distribution of values will be less than optimal for very large ranges.

Here's something that might be of interest - I dug this out of my trusty ol 'Programming the Pet/CBM' by Raeto Collin West (best book I ever bought) - its a Gaussian formula for generating pseudo random numbers, of the form x = a * x + c (mod m) - (should be subscripts on the x's - but you get the idea - x is plugged back into the formula to get the next x).

According to Gauss, the maximum period is obtained when a = 4*n + 1 and c is an odd number. I don't know this stuff well enough to tell you whether there is a particular a and c that should be used.

This little fn seems to give good results:

unsigned int GetRandInt32() {
    static unsigned x = 0;
    unsigned n = 5;   
    x = (((4*n+1) * x) + 1231) % INT_MAX;
    return x;
}


You can play with the values I have hardcoded (shame on me). There's probably better stuff on the web somewhere.

Anyway, the point here is that if you can roll your own rand that gives you values from 0 to INT_MAX you'll probably get a better distribution over the range -INT_MAX to INT_MAX than you will with the current rand(), whose RAND_MAX is 32767 - there's gotta be a loss of precision somewhere there.

There may be other considerations - some rands are prone to repeating subseries, some have quirks in that numbers generated alternate between even and odd. Also, there is the concept of a seed - how do we get a fundamentally different sequence out of this?

I also wonder if its a hard problem to predict the period given the inputs - I think it might be - though Gauss could probably assure inputs that give the maximum period - I think 257*x + 43981 (mod 65536) recurs after 65536 iterations.

Ok - now somebody's going to tell me about a good 5000 page academic text I should go read... sigh... anyway, just thought you might be interested...
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 
GeneralDooh! Pin
Jim Howard22-Jun-01 8:12
Jim Howard22-Jun-01 8:12 

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.