a very large interval of rundom numbers

This is a discussion on a very large interval of rundom numbers within the C++ Programming forums, part of the General Programming Boards category; Hi, I need a lot of random numbers, and her interval must is very large. For example I need generate ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    10

    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.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >This run, but there are many repeated or similar numbers.
    Then you apparently don't want random numbers.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    10
    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.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> (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.
    Last edited by Daved; 08-01-2006 at 03:37 PM.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.
    My best code is written with the delete key.

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    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)
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    10
    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.
    Help me please.
    Last edited by molistok; 08-01-2006 at 05:50 PM.

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    Maybe i'm invisible...
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #9
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Why is there a blank space between Prelude's and molistok's, it looks like there should a post there!

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    Hush
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> 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?

  12. #12
    Registered User
    Join Date
    Aug 2006
    Posts
    10
    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.
    Last edited by molistok; 08-02-2006 at 09:58 AM.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    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.

  14. #14
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    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.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  15. #15
    Registered User
    Join Date
    Aug 2006
    Posts
    10
    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:
    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).
    Thanks for the replys.
    Last edited by molistok; 08-02-2006 at 02:44 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Handling Large Numbers
    By Xzyx987X in forum C Programming
    Replies: 2
    Last Post: 05-03-2004, 03:33 PM
  2. Using really big numbers!
    By Machewy in forum C++ Programming
    Replies: 11
    Last Post: 02-26-2004, 10:49 AM
  3. Help needed with VERY large numbers.
    By jwarner in forum C++ Programming
    Replies: 4
    Last Post: 01-18-2004, 12:01 PM
  4. large numbers
    By Alextheking in forum C++ Programming
    Replies: 13
    Last Post: 01-09-2004, 03:51 PM
  5. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21