Solved

Want higher a higher value than RAND_MAX?

Posted on 1998-12-18
13
466 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
  • 6
  • 3
  • 3
  • +1
13 Comments
 

Author Comment

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

Accepted Solution

by:
Answers2000 earned 50 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
 

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now