Random-numbers...?

Discussion in 'C Programming' started by Koppe74, Sep 21, 2007.

  1. Koppe74

    Koppe74 Guest

    Sorry for this newbee question, but here goes...

    I of course knows about the rand()-function, but are there
    any functions that yields an integer randomnumber
    between 0 (or 1) and a *programmer defined* number - e.g.
    between 0 and 47 ? I tried redefining RAND_MAX but
    that obviously wasn't the way to do it...

    Alternatively a function returning a random float between
    0 and 1 could be used...

    As a last ditch, do anybody have a function that uses
    rand() and RAND_MAX to "translate" rand()'s random-
    numbers to any of the above... maybe also improving
    the "randomness" ? I need to make random-numbers
    with a value between 1 and 30-to-50, so it obviously
    need to fairly random in those ranges...

    -Koppe
    Koppe74, Sep 21, 2007
    #1
    1. Advertising

  2. Koppe74

    Mark Bluemel Guest

    Koppe74 wrote:
    > Sorry for this newbee question, but here goes...


    That's OK, but you should look at the FAQ - http://c-faq.com - before
    asking newbie questions...

    > I of course knows about the rand()-function, but are there
    > any functions that yields an integer randomnumber
    > between 0 (or 1) and a *programmer defined* number - e.g.
    > between 0 and 47 ?


    That's FAQ 13.6 - http://c-faq.com/lib/randrange.html
    Mark Bluemel, Sep 21, 2007
    #2
    1. Advertising

  3. Koppe74

    LPoD Guest

    Koppe74 pisze:
    > Sorry for this newbee question, but here goes...
    >
    > I of course knows about the rand()-function, but are there
    > any functions that yields an integer randomnumber
    > between 0 (or 1) and a *programmer defined* number - e.g.
    > between 0 and 47 ? I tried redefining RAND_MAX but
    > that obviously wasn't the way to do it...



    RTFFAQ:

    http://c-faq.com/lib/randrange.html


    ch.
    LPoD, Sep 21, 2007
    #3
  4. LPoD <> writes:
    > Koppe74 pisze:
    >> Sorry for this newbee question, but here goes...
    >> I of course knows about the rand()-function, but are there
    >> any functions that yields an integer randomnumber
    >> between 0 (or 1) and a *programmer defined* number - e.g.
    >> between 0 and 47 ? I tried redefining RAND_MAX but
    >> that obviously wasn't the way to do it...

    >
    > RTFFAQ:
    >
    > http://c-faq.com/lib/randrange.html


    RTFFAQ yourself (the first 'F' stands for "Fine", of course):

    [Note to web authors, catalogers, and bookmarkers: the URL
    <http://www.c-faq.com/> is the right way to link to these
    pages. All other URL's implementing this collection are subject to
    change.]

    When I cite the FAQ, I always post the base URL of the FAQ and the
    relevant question number. This avoids problems if the URLs are
    changed. It also encourages (well, forces) the reader to start at the
    front page and navigate to the relevant question, and perhaps learn
    something on the way.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Sep 21, 2007
    #4
  5. Koppe74

    Tor Rustad Guest

    Koppe74 wrote:

    [...]

    > As a last ditch, do anybody have a function that uses
    > rand() and RAND_MAX to "translate" rand()'s random-
    > numbers to any of the above... maybe also improving
    > the "randomness" ?


    Well, rand() isn't a RNG, given the same seed, it will be very much
    deterministic. :)

    Improving a bad PRNG, by some private tricks, is usually a bad idea, you
    are better off by using another PRNG.

    True RNG (TRNG) are more interesting and are typically biased. A method
    to unbias TRNG, lets consider a bit stream:

    if pair of bits are equal, consider next pair
    if pair of bits are different, emit previous pair.


    --
    Tor <torust [at] online [dot] no>
    Tor Rustad, Sep 21, 2007
    #5
  6. Koppe74

    Tor Rustad Guest

    Tor Rustad wrote:

    [...]

    > True RNG (TRNG) are more interesting and are typically biased. A method
    > to unbias TRNG, lets consider a bit stream:
    >
    > if pair of bits are equal, consider next pair
    > if pair of bits are different, emit previous pair.


    Ooops... change last line to:

    if pair of bits are different, emit 1 or 0


    --
    Tor <torust [at] online [dot] no>
    Tor Rustad, Sep 23, 2007
    #6
  7. Koppe74

    Thad Smith Guest

    Tor Rustad wrote:
    > Tor Rustad wrote:


    >> True RNG (TRNG) are more interesting and are typically biased. A
    >> method to unbias TRNG, lets consider a bit stream:
    >>
    >> if pair of bits are equal, consider next pair
    >> if pair of bits are different, emit previous pair.

    >
    > Ooops... change last line to:
    >
    > if pair of bits are different, emit 1 or 0


    By "emit 1 or 0" do you mean "emit 1 or 0, randomly chosen"? ;-)

    It would work to emit the second bit of the pair.


    --
    Thad
    Thad Smith, Sep 24, 2007
    #7
  8. Koppe74

    Tor Rustad Guest

    Thad Smith wrote:
    > Tor Rustad wrote:
    >> Tor Rustad wrote:

    >
    >>> True RNG (TRNG) are more interesting and are typically biased. A
    >>> method to unbias TRNG, lets consider a bit stream:
    >>>
    >>> if pair of bits are equal, consider next pair
    >>> if pair of bits are different, emit previous pair.

    >>
    >> Ooops... change last line to:
    >>
    >> if pair of bits are different, emit 1 or 0

    >
    > By "emit 1 or 0" do you mean "emit 1 or 0, randomly chosen"? ;-)


    LOL

    > It would work to emit the second bit of the pair.


    Yes. Alternatively, emit the first bit of the pair. The technique is due
    to von Neumann, since lots of bits will be discarded, more efficient
    methods exists.

    In practice, I think most implementations rather use a hashing scheme,
    even if it isn't a provable method for un-skrewing a bit-sequence. Also,
    there are methods based on Fast Fourier Transforms.


    --
    Tor <torust [at] online [dot] no>
    Tor Rustad, Sep 24, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. globalrev
    Replies:
    4
    Views:
    742
    Gabriel Genellina
    Apr 20, 2008
  2. Alex Untitled
    Replies:
    11
    Views:
    648
    Giampiero Zanchi
    Nov 16, 2009
  3. PerlFAQ Server

    FAQ 4.10 Why aren't my random numbers random?

    PerlFAQ Server, Feb 12, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    210
    PerlFAQ Server
    Feb 12, 2011
  4. PerlFAQ Server

    FAQ 4.10 Why aren't my random numbers random?

    PerlFAQ Server, Apr 27, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    239
    PerlFAQ Server
    Apr 27, 2011
  5. VK
    Replies:
    15
    Views:
    1,116
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page