Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Want higher a higher value than RAND_MAX?

Posted on 1998-12-18
13
Medium Priority
?
514 Views
Last Modified: 2011-09-20
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?
0
Comment
Question by:maj030598
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 3
  • +1
13 Comments
 

Author Comment

by:maj030598
ID: 1180538
Edited text of question
0
 
LVL 8

Accepted Solution

by:
Answers2000 earned 150 total points
ID: 1180539
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
 
LVL 84

Expert Comment

by:ozo
ID: 1180540
That answer looks familiar...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:maj030598
ID: 1180541
to answer2000:
How can I avoid negative numbers, I just want +ve numbers?
0
 

Author Comment

by:maj030598
ID: 1180542
to answer2000:
How can I avoid negative numbers, I just want +ve numbers?
0
 

Author Comment

by:maj030598
ID: 1180543
to answer2000:
How can I avoid negative numbers, I just want +ve numbers?
0
 

Author Comment

by:maj030598
ID: 1180544
to answer2000:
How can I avoid negative numbers, I just want +ve numbers?
0
 
LVL 11

Expert Comment

by:alexo
ID: 1180545
Well Ozo, there's this "joint copyright" clause in the EE agreement so you cannot complain...
0
 
LVL 84

Expert Comment

by:ozo
ID: 1180546
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 Comment

by:maj030598
ID: 1180547
can you please explain, what does the oring means?
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1180548
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
 
LVL 84

Expert Comment

by:ozo
ID: 1180549
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
 
LVL 8

Expert Comment

by:Answers2000
ID: 1180550
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question