random numbers..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • agsupriya86
    New Member
    • Mar 2007
    • 34

    random numbers..

    hello.

    I want to write my own random number generator..can anyone help me how to approach the problem??
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #2
    Originally posted by agsupriya86
    hello.

    I want to write my own random number generator..can anyone help me how to approach the problem??
    Hi,

    Have you searched on google? Also see this

    Kindly paste what you have done so far. Then only members can help you.

    Regards

    Comment

    • ravenspoint
      New Member
      • Jul 2007
      • 111

      #3
      zodilla58: He doesn't have any code to post - he doesn't know how to begin. If you want to be a policeman, go somewhere else.

      OP: The first question you have to ask is, what kind of random numbers do you want?

      There are real random numbers, which no-one can predict. They have to be obtained from some physical process, like tossing a coin and storing the results, or looking at the least significant bit of a clock.

      Or there are psuedo random numbers, which are perfectly predictable but seem to nbe random at a glance. These are generated using a mathematical function.

      Comment

      • Rasputin
        New Member
        • Jun 2007
        • 33

        #4
        If you include the standard library there is the function random(someInte ger), that is going to return an integer from 0 to someInteger.

        I believe it's the second type of random function generator that ravenspoint posted (mathematical).

        Ras.

        Comment

        • MMcCarthy
          Recognized Expert MVP
          • Aug 2006
          • 14387

          #5
          Originally posted by ravenspoint
          zodilla58: He doesn't have any code to post - he doesn't know how to begin. If you want to be a policeman, go somewhere else.
          ravenspoint

          zodilla58 is only operating under the guidelines of this site. We don't believe in spoonfeeding answers to posters. We are here to help them learn. Asking for clarification from a poster including what code, etc. they have already attempted is standard practice.

          Also if there is a belief that the poster is asking homework or coursework question then there are very clear guidelines as to how these questions should be answered. One of which is that full code answers should not be posted in answer to these questions.

          Also I would be grateful if you didn't make personal remarks to other members about how they should or shouldn't answer a post. If you have a problem with how another member deals with a post then raise the issue politely or send a PM to a Moderator / Admin.

          This is a technical community of developers and in general we get along with very little conflict.

          Mary
          ADMIN

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by ravenspoint
            zodilla58: ... If you want to be a policeman, go somewhere else.

            ...
            I don't think that line was neccessary.
            There's nothing wrong with zody's response. There was no way that zody could have guessed whether the OP had any code or not and so asking for the OP's code was not wrong.
            I hope zody was not offended with that, and of course you'll try not to offend others as well in the future, eh, ravenspoint.

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by mmccarthy
              ravenspoint

              zodilla58 is only operating under the guidelines of this site. We don't believe in spoonfeeding answers to posters. We are here to help them learn. Asking for clarification from a poster including what code, etc. they have already attempted is standard practice.

              Also if there is a belief that the poster is asking homework or coursework question then there are very clear guidelines as to how these questions should be answered. One of which is that full code answers should not be posted in answer to these questions.

              Also I would be grateful if you didn't make personal remarks to other members about how they should or shouldn't answer a post. If you have a problem with how another member deals with a post then raise the issue politely or send a PM to a Moderator / Admin.

              This is a technical community of developers and in general we get along with very little conflict.

              Mary
              ADMIN
              Since when did you get a faster keyboard than mine?

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                Originally posted by agsupriya86
                I want to write my own random number generator..can anyone help me how to approach the problem??
                Step 1, research the topic, this is fairly easy to do, for a topic like random and pseudo random numbers I would start on Wikipedia.

                Step 2, choose an algorthim, also not particularly hard as you will find when you look round Wikipedia that there is a plethera of information available and many of the algoithms have pseudo coded examples.

                Step 3, Implement the algorthim probably the hardest part but may still be quite easy. Take your pseudo coded algorithm, convert it to compilable C/C++ code. Post here if you run into difficulties.

                Comment

                • Rasputin
                  New Member
                  • Jun 2007
                  • 33

                  #9
                  Originally posted by Banfa
                  Step 1, research the topic, this is fairly easy to do, for a topic like random and pseudo random numbers I would start on Wikipedia.

                  Step 2, choose an algorthim, also not particularly hard as you will find when you look round Wikipedia that there is a plethera of information available and many of the algoithms have pseudo coded examples.

                  Step 3, Implement the algorthim probably the hardest part but may still be quite easy. Take your pseudo coded algorithm, convert it to compilable C/C++ code. Post here if you run into difficulties.
                  This is some pretty good answer. But sometimes I wonder if it's actually worth it to answer such posts and spend much more energy than the initial poster seems to have put.

                  Ras.

                  Comment

                  • Banfa
                    Recognized Expert Expert
                    • Feb 2006
                    • 9067

                    #10
                    Originally posted by Rasputin
                    This is some pretty good answer. But sometimes I wonder if it's actually worth it to answer such posts and spend much more energy than the initial poster seems to have put.
                    If they learn the methodology of how to set about tackling a problem and can apply it to their next problem then it certainly is. Not being clairevoyant I have to assume that they might learn if told however they certainly can not learn if not told so I tell'em and hope for the best.

                    Additionally while looking up information related to their problem there is always the chance that I will come accros something interesting that I was unaware of before so I benefit too.

                    And finally this is in fact a very inteligent question, the OP has not asked us to solve his actual problem (writing a random number generator), they have asked us how they can solve their own problem and is therefore definately worthy of an answer.

                    Comment

                    • emaghero
                      New Member
                      • Oct 2006
                      • 85

                      #11
                      Originally posted by Rasputin
                      If you include the standard library there is the function random(someInte ger), that is going to return an integer from 0 to someInteger.

                      I believe it's the second type of random function generator that ravenspoint posted (mathematical).

                      Ras.
                      The standard random number generator isn't very good. I would not suggest ever using it.

                      The best way to create a list of random numbers is to use a rng, unless you have a radioactive sample lying around andf you want to count the times between decays. The simplest method of generating random numbers is to use what's known as the Linear Congruential Generator (LCG). This will create a sequence of uniformly distributed random numbers.

                      As pointed out, this is not, strictly speaking, a set of truly random numbers. This numbers will become periodic with period 2^{m}.

                      The equation which defines the sequence is given by

                      x_{n} = a x_{n-1} + b mod M

                      To have numbers which are periodic with period 2^{m} define the parameters as follows

                      a = 1 mod 4
                      b = 1 mod 2
                      M = 2^{m}

                      m is any number natural number. Starting with the seed x_{0}, a sequence of random numbers can be generated. The larger the value for m the better because you will not have repition. Other choices for a, b and M exist but these should get you started.

                      Comment

                      • ravenspoint
                        New Member
                        • Jul 2007
                        • 111

                        #12
                        Originally posted by emaghero
                        The standard random number generator isn't very good. I would not suggest ever using it.
                        That is so harsh!

                        In fact the standard generator is free, widely available and well understood. These are important benefits not shared by a home brewed generator. They are so important benefits, that I would suggest using the standard generator, unless someone can show a very good reason why it would be the wrong choice.

                        If you routinely reseed the generator from the least significant digits of the system clock, then the standard generator serves perfectly for a huge number of the common purposes.

                        The classic example is selecting a wait time before again requesting a shared resource that is currently busy. It is does not matter what wait time is selected, just that it be 'random'. Obscure flaws in the statistical behaviour of the standard generator over long runs are irrelevant - if you have to back-off more than 2 - 3 times your system has problems that will not be solved with a 'better' generator.

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          To add to Emaghero's very fine post: for a bit of theory behind this all have a look
                          at Donald Knuth's "The Art Of Computer Programming".

                          If I'm not mistaken it's vol 2 "Seminumeri cal Algorithms" where all this is shown
                          and proven (there are quite a few more pseudo random number generators in
                          there too).

                          kind regards,

                          Jos

                          Comment

                          • JosAH
                            Recognized Expert MVP
                            • Mar 2007
                            • 11453

                            #14
                            Originally posted by ravenspoint
                            That is so harsh!

                            In fact the standard generator is free, widely available and well understood. These are important benefits not shared by a home brewed generator. They are so important benefits, that I would suggest using the standard generator, unless someone can show a very good reason why it would be the wrong choice.

                            If you routinely reseed the generator from the least significant digits of the system clock, then the standard generator serves perfectly for a huge number of the common purposes.
                            'common purposes' often go beyond a simple 'getting a number for which you
                            don't understand how your PC got it'. Reseeding the 'standard' pseudo random
                            number generator severly breaks its uniform distribution; it is not an option for
                            a lot of 'common purposes'.

                            Have a look at GNU's implementation for a much better pseudo random number
                            generator; it is free also and considered quite good; much better than the
                            'standard' implementation, widely used and available and well understood by
                            those who have a 'common use' for it.

                            kind regards,

                            Jos

                            Comment

                            • ravenspoint
                              New Member
                              • Jul 2007
                              • 111

                              #15
                              By free, I did not mean that you did not have to write a check.

                              I meant that you do not have to select an generator, install it, link it to your program, distribute it to your clients, and document why you chose it. This is all a huge amount of work for anyone who is not paid by the hour.

                              The standard generator is just there, free.

                              Comment

                              Working...