Random number generation from functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • drs

    Random number generation from functions

    Is there any way to generate random numbers based on arbitrary real valued
    functions? I am looking for something like random.gauss() but with natural
    log and exponential functions.

    thanks,

    -d


  • Bengt Richter

    #2
    Re: Random number generation from functions

    On Mon, 29 Nov 2004 20:51:50 GMT, "drs" <drs@remove-to-send-mail-ecpsoftware.com > wrote:
    [color=blue]
    >Is there any way to generate random numbers based on arbitrary real valued
    >functions? I am looking for something like random.gauss() but with natural
    >log and exponential functions.
    >
    >thanks,[/color]
    Don't know what you mean. This kind of thing?
    (the 1-random... is to make the random numbers in (0,1] instead of [0,1))
    [color=blue][color=green][color=darkred]
    >>> import random
    >>> import math
    >>> math.log(1.0-random.uniform( 0,1))[/color][/color][/color]
    -0.9003028845584 1156[color=blue][color=green][color=darkred]
    >>> math.log(1.0-random.uniform( 0,1))[/color][/color][/color]
    -0.2612491418648 35[color=blue][color=green][color=darkred]
    >>> math.log(1.0-random.uniform( 0,1))[/color][/color][/color]
    -0.9969436681854 7997

    Regards,
    Bengt Richter

    Comment

    • Colin J. Williams

      #3
      Re: Random number generation from functions

      drs wrote:[color=blue]
      > Is there any way to generate random numbers based on arbitrary real valued
      > functions? I am looking for something like random.gauss() but with natural
      > log and exponential functions.
      >
      > thanks,
      >
      > -d
      >
      >[/color]
      numarray has a random package which provides a number of functions,
      including: normal( mean, stddev, shape=[])

      That may serve your need.

      Colin W.

      Comment

      • CptPicard

        #4
        Re: Random number generation from functions

        drs wrote:[color=blue]
        > Is there any way to generate random numbers based on arbitrary real valued
        > functions? I am looking for something like random.gauss() but with natural
        > log and exponential functions.
        >
        > thanks,
        >
        > -d
        >
        >[/color]
        I remember for having used it on a gaussian generator that you can do that very
        easily by yourself:
        - you generate a random real number in the range [0 .. 1],
        - then you apply an inverse probability density function (e.g. inverse gaussian
        pdf : http://www.itl.nist.gov/div898/softw...ar/rigpdf.htm),
        and you have then your random number following the specified law (here gaussian).

        Comment

        • Robert Kern

          #5
          Re: Random number generation from functions

          drs wrote:[color=blue]
          > Is there any way to generate random numbers based on arbitrary real valued
          > functions? I am looking for something like random.gauss() but with natural
          > log and exponential functions.[/color]

          scipy[1] has a large collection of "standard" univariate pdfs, including
          normal, exponential, gamma, and the like.

          One cannot generate random numbers from arbitrary real valued functions
          because a function needs to satisfy certain restrictions to be a pdf.
          For example, you cannot generate random numbers "with a log function",
          unless you restrict the domain and renormalize.

          Generating random numbers from an arbitrary pdf is possible, but tricky.
          I encourage you to search the literature for "monte carlo sampling."

          [1] http://www.scipy.org

          --
          Robert Kern
          rkern@ucsd.edu

          "In the fields of hell where the grass grows high
          Are the graves of dreams allowed to die."
          -- Richard Harter

          Comment

          • Alejandro López-Valencia

            #6
            Re: Random number generation from functions

            On Mon, 29 Nov 2004 20:51:50 GMT, "drs"
            <drs@remove-to-send-mail-ecpsoftware.com > wrote:
            [color=blue]
            >Is there any way to generate random numbers based on arbitrary real valued
            >functions? I am looking for something like random.gauss() but with natural
            >log and exponential functions.[/color]

            Try with CRNG, it may have what you need, or be able to adapt it to
            what you want. http://www.sbc.su.se/~per/crng/

            Comment

            Working...