This course teaches how to install and configure Windows Server 2012 R2. It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

RAND_MaX is set to 32767. How can set the limit higher? In other words, I want to be able to select a unique 100,000 numbers from 500,000, how can I do that?

unsigned int rnd = ( (r1) | (r2 << 15) | ( r3 << 30 ) ) ;

/* I meant to click the "Comment" button, not the "Complain" button... */

r1 is a 15 bit random number

r2 is a 15 bit rnadom number

r3 is a 2 bit (original version) or 1 bit (ozo's modification) random number

The shifts are by 15 and 30 move r2 and r3, 15 and 30 places (binary digit places) to respectively. The | combine these numbers, into one big number

e.g.

A 32 bit random number, has 32 binary digits. These are filled out based on r1, r2 and r3, based on this

33222222222222222111111111

03222222222222222111111111

where 3 = a binary digit coming from r3

where 2 = a binary digit coming from r2

where 1 = a binary digit coming from r1

where 0 = a binary digit which is always 0

Ozo's modified version works because to remove negative numbers by ensuring the first digit is always 0 (the first digit being 1 means a negative, 0 means a postive, the sign bit).

Incidentally as I read Ozo's modification, by changing the int type to unsigned int, the change to r3 is not necessary (it's only necessary when working with signed ints - unsigned ints don't have sign bits, they are always positive).

depending on what maj meant.

It looks like the idea may have been to take rnd%500000

(which mathematically ought to be positive regardless of the sign of rnd, but the C++ committee chose to codify a commonly implemented misseature instead)

But another way to choose 100,000 random elements out od 500,000 could be to go through each of the 500000 elements in turn and select it with probability (100000-number_of_elements

A 15 bit rand() won't generate that probabilty exactly, but then, the crude method won't really give a random 31 bit number either, because of correlations beween r1 and r2,

and even if it did, since 500000 is not a power of 2, taking it mod 500000 wouldn't be perfectly distributed either. (but if you're using the crude method, that probably doesn't matter too much to you anyway)

My understanding of a good random generator is one which has zero correlation between successive numbers. Of course the typical formulas used in the runtime library aren't that good. This also leads to question about what formula is used by maj's compiler runtime library (which could be VC - RAND_MAX in VC5 is 32767).

Your point about modulo is well taken and correct (as usual Ozo)

BTW Ozo - have you see the lounge question where you came up - some experts are wondering if you are an AI ? :-)

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

For VC RAND_MAX is 0x7FFF as you say

// example of a 32 bit random number

int r1 = rand() & 0x7FFF ;

inr r2 = rand() & 0x7FFF ;

int r3 = rand() & 0x0003 ;

int rnd = ( (r1) | (r2 << 15) | ( r3 << 30 ) ) ;

2. Alternatively see

http://www.math.keio.ac.jp/~nisimura/random/int/mt19937int.c

for a less crude method