# 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?
###### Who is Participating?

Commented:
1. The crude way to do this is to make multiple calls to rand() and then shift the result of successive values.  You can mod (%) the value by the ( max possible value - min possible value ) then add the min possible value.

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
0

Author Commented:
Edited text of question
0

Commented:
0

Author Commented:
How can I avoid negative numbers, I just want +ve numbers?
0

Author Commented:
How can I avoid negative numbers, I just want +ve numbers?
0

Author Commented:
How can I avoid negative numbers, I just want +ve numbers?
0

Author Commented:
How can I avoid negative numbers, I just want +ve numbers?
0

Commented:
Well Ozo, there's this "joint copyright" clause in the EE agreement so you cannot complain...
0

Commented:
int r3 = rand() & 0x0001 ;
unsigned int rnd = ( (r1) | (r2 << 15) | ( r3 << 30 ) ) ;

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

Author Commented:
can you please explain, what does the oring means?
0

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

0

Commented:
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)
0

Commented:
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 ? :-)
0
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.