# a very large interval of rundom numbers

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 08-01-2006
molistok
a very large interval of rundom numbers
Hi,

I need a lot of random numbers, and her interval must is very large. For example I need generate a numbers between 6.79*10-8 to 1.0 and I want that they are a lot of numbers differents.

I now generate these random numbers with this algorithm:
Code:

```srand (time(NULL)); double value; for (i;i<1000000;i++){ value=((MI + rand() % (int)(total-MI))/total); }```
(total=14709200 and MI=1, 1/14709200=6.79*10-8)
This run, but there are many repeated or similar numbers. Anybody have one idea to improve this rundom numbers?

Thanks.
• 08-01-2006
Prelude
>This run, but there are many repeated or similar numbers.
Then you apparently don't want random numbers.
• 08-01-2006
molistok
yes, I want a random numbers. (I am sorry my English is very poor I am from Barcelona).
I want random numbers, my algorithm generate 1000000 of numbers but for example the number: 0.000611386 is repeated 30 times. Also I have a lot of numbers very similars to this number and I don't want this. I want find one algorithm that I can generate more numbers differents. Can anybody help me please?thanks for the reply.
• 08-01-2006
Daved
>> (MI + rand() % (int)(total-MI))
If RAND_MAX is less than 14709199 (it is 32767 on my platform), then this will always be rand() + 1. Are you sure that is the formula you want? What is the rest of the code?

This also means that you can only get RAND_MAX distinct numbers. In my case, that means each value is repeated on average 30 times. If you are using a single call to rand() and RAND_MAX is only 32767, then you should expect only 32767 distinct values. If you iterate 1000000 times, then on average those numbers repeat 30 times each.
• 08-01-2006
Prelude
>I want random numbers, my algorithm generate 1000000 of numbers but for
>example the number: 0.000611386 is repeated 30 times.
You're contradicting yourself. A sequence could be 1 2 3 4 5 or 1 1 1 1 1 and still be completely random. What you seem to want is a non-repeating random permutation of evenly distributed numbers in a certain range. Since that's the case, set up an array and then permute it randomly. As an example:
Code:

```int rnd[100]; for ( int i = 0; i < 100; i++ )   rnd[i] = i * 100; std::random_shuffle ( rnd, rnd + 100 ); for ( int i = 0; i < 100; i++ )   std::cout<< rnd[i] <<' '; std::cout<<'\n';```
Random numbers will repeat. Random numbers will cluster. Those are two properties of randomness that people don't seem to expect. If you don't want that then you don't really want random numbers.
• 08-01-2006
Mario F.
I'm assuming the OP is having doubts about the distribution property of his current algorithm.

You may want to check this:
http://eternallyconfuzzled.com/libs/jsw_rand.html
(Prelude's implementation of Mersenne Twister)

Or this:
http://www-personal.engin.umich.edu/...neTwister.html
(Wagner's implementation of Mersenne Twister)
• 08-01-2006
molistok
Quote:

Random numbers will repeat. Random numbers will cluster. Those are two properties of randomness that people don't seem to expect. If you don't want that then you don't really want random numbers.
I understand that in a random numbers some numbers can repated, but I think that isn't normal that if I have a 14709199 numbers differents I with my algorithm generate 30 times the same number.

It is a list with the first random numbers that I have generated:
Code:

```0.00155936+0.000412735+0.000484663+0.00209189+0.00216429+0.00184374+ 0.000590991+0.00185856+0.000627022+0.00128457+0.00219162+0.00172953+ 0.000401585+0.00145344+0.00133209+0.00208196+0.000283496+0.000613221+ 0.000339855+0.00124324+0.00161504+0.000194164+0.00194368+4.34422e-005+ 0.000475757+0.00176876+0.00191859+0.00188181+1.3257e-005+0.00152537+0.000375139 +0.00141578+0.00121523+0.000409404+0.00168758+0.00064769+0.00059473+ 0.000206809+0.000903924+0.00096409+0.00209923+0.0015113+0.000147051+ 0.000991828+0.000429323+0.000581881+0.00161266+6.29538e-005+0.000996859+ 0.00103343+0.000622331+0.00150973+0.000293422+0.00172273+0.0010815+0.0022148+ 0.000810445+0.00127668+0.00105791+0.000509885+0.00210236+0.000676515+ 0.00201683+0.00216388+0.00203553+0.00201642+0.000570051+0.000861161+0.00105281 +0.000444959+0.000678011+0.00202078+0.00116825+0.000824382+0.000708604+ 0.00175149+0.00143414+0.000421029+0.00220195+0.000933429+0.0016436+0.000352568 +0.00142795+0.00186455+0.000416746+0.00046991+0.00167589+0.000630286+ 0.00166175+0.0003009```
(+This is to separate the different numbers of the list)
You can see that for example never generate one number similar to 0.9004 or 0.07 or 0.992, and I comprove this in the file that i stored the 1000000 numbers and I haven't any number that the start of number is similiar to 0.9,0.8,0.7,0.6,0.5,0.4,0.3,0.2. Any idea to solve this problem?Thanks for the replys.
• 08-01-2006
Mario F.
Maybe i'm invisible...
• 08-01-2006
Wraithan
Why is there a blank space between Prelude's and molistok's, it looks like there should a post there!
• 08-01-2006
Mario F.
Hush ;)
• 08-01-2006
Daved
>> Any idea to solve this problem?Thanks for the replys. Help me please.
All your numbers are between (1/14709200) and (32768/14709200) for the reasons I mentioned in my previous post.

What formula are you trying to achieve? What number range are you trying to achieve? Do you understand why out of your 1000000 values, you will only get 32767 actually different numbers?
• 08-02-2006
molistok
Thanks Mario F. and Daved I will study your replys after, I now I have seen very fast your replys (and your links), and I will try to change my algorithm because of that I now know that my algorithm isn't correct for that Daved say. And I will try to implement the MTrand of the links of Mario F. Thanks you very much, now I am working and I can't do this. Mario F you aren't invisible. I am sorry and thanks for the reply.

If you want paste one example of one application of your links you can do it, is a very big code. I will study it, but if you want paste one example I will be to you grateful.
• 08-02-2006
Daved
I don't think you need anything as complicated as what is in Mario F.'s links. Just answer the questions I asked. What range do you want? Do you understand that you will only get 32767 distinct numbers? If it is a simple range of [0,1], then you can use rand() / RAND_MAX. If you want MI and total to be involved in the random value, let us know how they should be involved.
• 08-02-2006
Mario F.
There is nothing complicated about both implementations of MT.

For Wagner's one for instance, just include the header file and write, for instance:

Code:

```MTRand test int result; result = test.RandInt(100); //produces a random integer from 0 to 100```
The whole class is clearly documented.
• 08-02-2006
molistok
For Daved:

Yes I understand that I will only get 32767 distinct numbers, but I need 14709200 numbers.
I need a MI and value variables in my algorithm because I want that my numbers are from 0 to 1, but I can't do rand() / RAND_MAX because after never I will generate numbers similars to: 1/14709200 (I need get numbers with these decimals),and I need get 14709200 numbers differents.

For Mario:
I go to try this:
Quote:

MTRand test;
double value;
int result;
result = test.randInt((int)total);
value=result/total; //produces a random integer from 0 to 1 with 14709200 numbers differents?
(total=14709200 for example).