Link to home
Start Free TrialLog in
Avatar of maj030598
maj030598

asked on

Want higher a higher value than RAND_MAX?

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?
Avatar of maj030598
maj030598

ASKER

Edited text of question
ASKER CERTIFIED SOLUTION
Avatar of Answers2000
Answers2000

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ozo
That answer looks familiar...
to answer2000:
How can I avoid negative numbers, I just want +ve numbers?
to answer2000:
How can I avoid negative numbers, I just want +ve numbers?
to answer2000:
How can I avoid negative numbers, I just want +ve numbers?
to answer2000:
How can I avoid negative numbers, I just want +ve numbers?
Well Ozo, there's this "joint copyright" clause in the EE agreement so you cannot complain...
int r3 = rand() & 0x0001 ;
unsigned int rnd = ( (r1) | (r2 << 15) | ( r3 << 30 ) ) ;

/* I meant to click the "Comment" button, not the "Complain" button... */
can you please explain, what does the oring means?
ozo you taught me something - learning is a reason why I visit EE!

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

33222222222222222111111111111111 (original version)
03222222222222222111111111111111 (Ozo's modified version)

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).









The two modifications could be two different ways to avoid negative numbers,
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_selected_so_far)/(500000-number_of_elements_tested_so_far)
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)
We're kind of getting off maj's question, but I was wondering about the correlations :

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 ? :-)