1. ## Random numbers

I have found out a code that can give random numbers but I think this code gives a random number between 0-32767.
Could it be possible to get a random number between two values like for example: 0-30 ?

Code:
```#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
int random_integer = rand();
cout << random_integer << endl;
}```

2. Yes
Code:
`int random_integer = rand() &#37; 31;`

3. Thanks for the guidance
Code:
```int test = 0;

test = rand() &#37; 30;```

4. If you do % 30 you will get from 0-29. Since if rand() is 30 you will get 0 not 30. So you need 31

5. I see, I didn&#180;t think about that. Thanks for the hint.

6. That said, I suggest reading Prelude's articles on Using rand() and Random Numbers.

7. This is generalyl a better random number generator than rand(). It only generates random bytes, so just generat 4 random bytes into a DWORD and then apply the aforementioned % 31.

8. Originally Posted by abachler
This is generalyl a better random number generator than rand(). It only generates random bytes, so just generat 4 random bytes into a DWORD and then apply the aforementioned &#37; 31.
And of course, you know that franse needs a better random number generator from what has been discussed so far?

And "better" is of course a matter of how you defeine "better". "good" random numbers are important if you are working on something where the quality of the random numbers have a commercial consequence.

DWORD is not a standard type in anything other than Windows.

Doing a loop of 64K for EVERY BYTE seems excessive. Is this actually a recognized method of generating random numbers, or just something you thought up yourself? There are a huge number of GOOD random number generators that have had years of research to prove that they are good, the Mersenne-Twister being one of those: http://en.wikipedia.org/wiki/Mersenne_Twister. It uses less memory and less, and only loops around for 624 iterations every 624th random number - which is about 600 * 100 times better than your code.

--
Mats

9. DWORD is just a macro for unsigned int, to say 'well its only available in windows' is a bit facesious, since windows is argueably the most important operating system in existance today.

Besides, you can reduce the 64K loop to much smaller loop and still have random numbers of high enough quality for your purpose. Personally I use a much larger loop for some projects.

And mersenne twisters are very poor quality, they repeat in reasonable time. My code will not repeat during the lifetime of the universe, not even close.

10. Originally Posted by abachler
DWORD is just a macro for unsigned int, to say 'well its only available in windows' is a bit facesious, since windows is argueably the most important operating system in existance today.
And that is a good reason, you say? Why not use a typedef of your own that equates to unsigned int? - that way, the code is genuinely portable.

And mersenne twisters are very poor quality, they repeat in reasonable time. My code will not repeat during the lifetime of the universe, not even close.

So you have evidence to say that the MT paper:
http://www.math.sci.hiroshima-u.ac.j...earticles.html
that describes the initial implementation is not only wrong, but considerably wrong? According to that paper it has a period (which I take to be the point where it repeats) of 2^19937 - which is certainly considerably longer than I can expect to live. So where did they go wrong, seeing as this is a peer-reviewed paper, and implemented for MANY platforms in different languages - and all of the people that have been using it over the last 10 years are obviously not aware of this deficiency. Are you planning on to release a paper with your findings, so that we can learn what you know?

--
Mats

11. Originally Posted by abachler
DWORD is just a macro for unsigned int, to say 'well its only available in windows' is a bit facesious, since windows is argueably the most important operating system in existance today.
Nonetheless, it is not the only operating system in actual use today, and in fact is not dominant in some areas of computing.

Originally Posted by abachler
And mersenne twisters are very poor quality, they repeat in reasonable time.
That is weird: all the literature I have read on it disagrees with you, and in fact state that the Mersenne Twister algorithms are high quality pseudo-random number generators for non-cryptographic use.

Originally Posted by abachler
My code will not repeat during the lifetime of the universe, not even close.
However, that does not prove that it is a high quality pseudo-random number generator, e.g., a generator that merely increments the previously generated number will also theoretically never repeat (for eternity), but obviously it does not have very "random" characteristics. Is this algorithm in the published literature?

12. Originally Posted by laserlight
for non-cryptographic use.
Precisely.

13. Originally Posted by abachler
Precisely.
Then you are mistaken: there are valid uses for non-cryptographic quality PRNGs, e.g., for simulations.

How does the PRNG you suggested compare to say, Blum Blum Shub?

14. Originally Posted by laserlight
Then you are mistaken: there are valid uses for non-cryptographic quality PRNGs, e.g., for simulations.

How does the PRNG you suggested compare to say, Blum Blum Shub?
It was developed from similar foundations, but uses different methods. It has a cryptographic strength of greater than 512K bits.

15. Originally Posted by abachler
It was developed from similar foundations, but uses different methods.
I see. What publications are available that describe it and provide analyses of its quality?

Originally Posted by abachler
It has a cryptographic strength of greater than 512K bits.
It may be due to my lack of expertise in this area, but that does not make sense to me. What does it mean for a cryptographically secure PRNG to have "a cryptographic strength of greater than 512K bits"?