Professional Web Applications Themes

Need help with "Random" - Mac Programming

I am a newbie and currently developing a Cocoa application that requires the function "Random". I have already written most of the code and it works fine. My main problem is that I do not know where to declare the variable "randSeed". According to the doentation it should be declared as a global variable. I have already created an appropriate init for the variable (on launch it writes the current date and time into the variable). However "Random" always returns the same sequence of pseudorandom numbers after launch. Where do I declare the global variable "randSeed"?...

Sponsored Links
  1. #1

    Default Need help with "Random"

    I am a newbie and currently developing a Cocoa application that
    requires the function "Random". I have already written most of the
    code and it works fine.

    My main problem is that I do not know where to declare the variable
    "randSeed". According to the doentation it should be declared as a
    global variable. I have already created an appropriate init for the
    variable (on launch it writes the current date and time into the
    variable). However "Random" always returns the same sequence of
    pseudorandom numbers after launch.

    Where do I declare the global variable "randSeed"?
    Sponsored Links
    ro86 Guest

  2. #2

    Default Re: Need help with "Random"

    In article <google.com>,
    com (ro86) wrote:
     

    You don't. Carbon does it for you. You want to call
    SetQDGlobalsRandomSeed( mySeedValue)
    <http://developer.apple.com/doentation/Carbon/Reference/QuickDraw_Ref/
    qdref_main/FunctionGroupIndex.html> to set it. The mention of 'randSeed'
    is a left-over from 1984, when there was a global called randSeed (See
    <http://developer.apple.com/doentation/mac/OSUtilities/OSUtilities-63.
    html>, which fails to mention that it is outdated (as I just read in
    some other post 'where is Caroline Rose when you need her?')

    Reinder
    Reinder Guest

  3. #3

    Default Re: Need help with "Random"

    In article <wxs.nl>, Reinder
    Verlinde <invalid> wrote:
     
    >
    > You don't. Carbon does it for you. You want to call
    > SetQDGlobalsRandomSeed( mySeedValue)
    > <http://developer.apple.com/doentation/Carbon/Reference/QuickDraw_Ref/
    > qdref_main/FunctionGroupIndex.html> to set it. The mention of 'randSeed'
    > is a left-over from 1984, when there was a global called randSeed (See
    > <http://developer.apple.com/doentation/mac/OSUtilities/OSUtilities-63.
    > html>, which fails to mention that it is outdated (as I just read in
    > some other post 'where is Caroline Rose when you need her?')[/ref]

    Also note that the behaviour of Random depends very much on what you
    do with SetQDGlobalsRandomSeed( mySeedValue).

    Generally, you should call this only once in your application
    (preferably in your startup sequence). You may also want to make sure
    that it is called with a different value of mySeedValue each time the
    application launches. The usual trick is to seed it with the current
    raw time value (_not_ the TickCount()!!) - the carbon docs should
    explain how to do that. If you call SetQDGlobalsRandomSeed with the
    same value of mySeedValue, you are forcing the Random function to start
    from the same point in the (pseudorandom) sequence.
    David Guest

  4. #4

    Default Re: Need help with "Random"

    On Wed, 26 May 2004, ro86 wrote:
     

    If you're using Cocoa (or even Carbon without needing OS 9 compatibility)
    you're probably better off using random(). You can seed it with srandom()
    or, if you want high-quality randomness and can depend on Panther, with
    srandomdev(). The interface is simple and random() probably gives
    better-quality randomness than Random(), as well.
    Michael Guest

  5. #5

    Default Re: Need help with "Random"

    In article <twistedsys.net>, Michael
    Ash <com> wrote:
     
    >
    > If you're using Cocoa (or even Carbon without needing OS 9 compatibility)
    > you're probably better off using random(). You can seed it with srandom()
    > or, if you want high-quality randomness and can depend on Panther, with
    > srandomdev(). The interface is simple and random() probably gives
    > better-quality randomness than Random(), as well.[/ref]

    There's also boost::random if you're using c++.

    www.boost.org
    Chris Guest

  6. #6

    Default Re: Need help with "Random"

    Chris Baum <net> wrote in message news:<260520041739531357%net>... 
    > >
    > > If you're using Cocoa (or even Carbon without needing OS 9 compatibility)
    > > you're probably better off using random(). You can seed it with srandom()
    > > or, if you want high-quality randomness and can depend on Panther, with
    > > srandomdev(). The interface is simple and random() probably gives
    > > better-quality randomness than Random(), as well.[/ref]
    >
    > There's also boost::random if you're using c++.
    >
    > www.boost.org[/ref]


    Thank you!

    I have decided to use srandomdev() like this:

    - (void)awakeFromNib
    {
    srandomdev();
    }

    It works very well. I only wonder why srandomdev() is not doented.
    I would have never found this by myself. (Does the "...dev" mean that
    it is still in Development?)
    Again, thanks!
    ro86 Guest

  7. #7

    Default Re: Need help with "Random"

    On Thu, 27 May 2004, ro86 wrote:
     

    It looks plenty doented to me. Open Terminal, type 'man srandomdev',
    and read the unix-y goodness. If you're not sure what you're looking for,
    you can find a mention of these functions by typing 'apropos random'.

    It has 'dev' in the name because it seeds the random number generator
    using the /dev/random random number device. There is also a plain
    srandom() function that lets you pass in your own value, but srandomdev()
    is preferred because it produces mugh higher-quality randomness.
    Michael Guest

  8. #8

    Default Re: Need help with "Random"


    "Michael Ash" <com> wrote in message
    news:twistedsys.net... 
    >
    > It looks plenty doented to me. Open Terminal, type 'man srandomdev',
    > and read the unix-y goodness. If you're not sure what you're looking for,
    > you can find a mention of these functions by typing 'apropos random'.
    >
    > It has 'dev' in the name because it seeds the random number generator
    > using the /dev/random random number device. There is also a plain
    > srandom() function that lets you pass in your own value, but srandomdev()
    > is preferred because it produces mugh higher-quality randomness.[/ref]

    Why don't you show us a test for randomness that srandomdev() passes, but
    srandom() fails.
    If there is no such test, how can one be of much higher quality than the
    other?


    Carsten Hansen


    Carsten Guest

  9. #9

    Default Re: Need help with "Random"

    In article <B1ouc.96113$ops.worldnet.att.net>,
    Carsten Hansen <att.net> wrote:
     
    > >
    > > It looks plenty doented to me. Open Terminal, type 'man srandomdev',
    > > and read the unix-y goodness. If you're not sure what you're looking for,
    > > you can find a mention of these functions by typing 'apropos random'.
    > >
    > > It has 'dev' in the name because it seeds the random number generator
    > > using the /dev/random random number device. There is also a plain
    > > srandom() function that lets you pass in your own value, but srandomdev()
    > > is preferred because it produces mugh higher-quality randomness.[/ref]
    >
    > Why don't you show us a test for randomness that srandomdev() passes, but
    > srandom() fails.
    > If there is no such test, how can one be of much higher quality than the
    > other?[/ref]


    I quote from the man page:

    The srandomdev() routine initialize a state array using random(4)
    random number device which returns good random numbers, suitable for
    cryptographic use. Note that this particular seeding procedure can
    generate states which are impossible to reproduce by calling srandom()
    with any value, since the succeeding terms in the state buffer are no
    longer derived from the LC algorithm applied to a fixed seed.

    However I do have a question about the man pages. I can find the above
    browsing through Apple's online doentation or by searching online
    manuals but in looking this up for this thread, I discovered that from
    Terminal, "man srandomdev" (or "man srandom") yields "No manual entry
    for srandomdev". Is there some additional installation that is needed
    to get all of the Darwin doentation?

    Spence

    --
    James P. Spencer
    Rochester, MN

    "Badges?? We don't need no stinkin badges!"
    James Guest

  10. #10

    Default Re: Need help with "Random"


    "James Spencer" <net> wrote in message
    news:300520041236234355%net... [/ref][/ref]
    doented. [/ref][/ref]
    that [/ref][/ref]
    srandomdev', [/ref][/ref]
    for, [/ref][/ref]
    srandomdev() 
    > >
    > > Why don't you show us a test for randomness that srandomdev() passes,[/ref][/ref]
    but 
    >
    >
    > I quote from the man page:
    >
    > The srandomdev() routine initialize a state array using random(4)
    > random number device which returns good random numbers, suitable for
    > cryptographic use. Note that this particular seeding procedure can
    > generate states which are impossible to reproduce by calling srandom()
    > with any value, since the succeeding terms in the state buffer are no
    > longer derived from the LC algorithm applied to a fixed seed.
    >
    > However I do have a question about the man pages. I can find the above
    > browsing through Apple's online doentation or by searching online
    > manuals but in looking this up for this thread, I discovered that from
    > Terminal, "man srandomdev" (or "man srandom") yields "No manual entry
    > for srandomdev". Is there some additional installation that is needed
    > to get all of the Darwin doentation?
    >
    > Spence
    >
    > --
    > James P. Spencer
    > Rochester, MN[/ref]

    You are providing no evidence that one seeding algorithm is better than the
    other.
    Think about it, if you initialize a linear congruential random number
    generator, does it matter whether the single seed comes from a
    cryptographically secure random number generator, or the seed comes from say
    the time function in C?

    Carsten Hansen


    Carsten Guest

  11. #11

    Default Re: Need help with "Random"

    On Sun, 30 May 2004, Carsten Hansen wrote:
     

    Easy:

    Write two routines. One routine calls srandom() with whatever value you
    want (time(), four bytes of /dev/random, etc.), then calls random(). The
    other routine calls srandomdev(), then calls random() several times to
    get a sequence. Run each of them until they start to repeat the same
    sequence as a previous run.

    Since srandom() takes an unsigned long, it can only have 2^32 possible
    states, so you will start producing repeats after at most four billion
    iterations, and change. If you're using time() to seed, then you only have
    to run it twice within the same second, of course.

    The srandomdev() function, seeds directly into random()'s internal state.
    The random() function can use anywhere from 8 to 256 bytes of internal
    state; the man page does not say how many bytes are used when calling
    srandomdev(), but it must be at least 8. So seeding with srandomdev() will
    give you nonrepeating sequences at least four billion times longer than
    srandom() will, and probably even more than that.

    (Reanding the libc source code, it appears that, unless initstate() is
    called to change it, srandom() will use a 128-byte internal state, and
    srandomdev() will fill 124 bytes of it with random goodness.)

    Of course, if all you're doing is providing randomized behavior for a
    game, it hardly matters.
    Michael Guest

  12. #12

    Default Re: Need help with "Random"

    On Sun, 30 May 2004, James Spencer wrote:
     

    You have to install the BSD Subsystem when installing the OS; it's
    optional, but on by default. I've also heard of people installing it, but
    still not getting man pages. In both cases, it can be resolved by
    reinstalling the BSD Subsystem from your OS X CDs.
    Michael Guest

  13. #13

    Default Re: Need help with "Random"


    "Michael Ash" <com> wrote in message
    news:twistedsys.net... [/ref]
    but 
    >
    > Easy:
    >
    > Write two routines. One routine calls srandom() with whatever value you
    > want (time(), four bytes of /dev/random, etc.), then calls random(). The
    > other routine calls srandomdev(), then calls random() several times to
    > get a sequence. Run each of them until they start to repeat the same
    > sequence as a previous run.
    >
    > Since srandom() takes an unsigned long, it can only have 2^32 possible
    > states, so you will start producing repeats after at most four billion
    > iterations, and change. If you're using time() to seed, then you only have
    > to run it twice within the same second, of course.
    >
    > The srandomdev() function, seeds directly into random()'s internal state.
    > The random() function can use anywhere from 8 to 256 bytes of internal
    > state; the man page does not say how many bytes are used when calling
    > srandomdev(), but it must be at least 8. So seeding with srandomdev() will
    > give you nonrepeating sequences at least four billion times longer than
    > srandom() will, and probably even more than that.
    >
    > (Reanding the libc source code, it appears that, unless initstate() is
    > called to change it, srandom() will use a 128-byte internal state, and
    > srandomdev() will fill 124 bytes of it with random goodness.)
    >
    > Of course, if all you're doing is providing randomized behavior for a
    > game, it hardly matters.[/ref]

    Wrong. The period of a random number generator is not dependent on the size
    of the seed, but only on the size of the internal state. Since the internal
    state (128 bytes) is independent of the seeding method, you are providing no
    evidence that one is superior to the other.

    Clearly you didn't try out your own suggestion of setting up a loop for
    detecting when random starts to repeat the same sequence.

    Carsten Hansen


    Carsten Guest

  14. #14

    Default Re: Need help with "Random"

    On Sun, 30 May 2004, Carsten Hansen wrote:
     

    Maybe I wasn't clear. I meant, write routines like this:

    #define SEQUENCE_SIZE 10

    void srandomtest(unsigned long seed, long sequence[SEQUENCE_SIZE]) {
    srandom(seed);
    for(int i = 0; i < SEQUENCE_SIZE; i++)
    sequence[i] = random();
    }

    void srandomdevtest(long sequence[SEQUENCE_SIZE]) {
    srandomdev()
    for(int i = 0; i < SEQUENCE_SIZE; i++)
    sequence[i] = random();
    }

    Then use a controlling program like this:

    void runtest(void) {
    while(1) {
    long sequence[SEQUENCE_SIZE];
    srandomtest(your_seed_generator_here(), sequence);
    if(already_seen(sequence))
    printf("srandom() repeated\n");
    srandomdevtest(sequence)
    if(already_seen(sequence))
    printf("srandomdev() repeated\n");
    }
    }
     

    That wasn't my suggestion. You're right that I didn't try my suggestion,
    though, because it will probably take several months or years to produce
    any results if you actually run it.
    Michael Guest

  15. #15

    Default Re: Need help with "Random"


    "Michael Ash" <com> wrote in message
    news:twistedsys.net... [/ref]
    size [/ref]
    internal [/ref]
    providing no 
    >
    > Maybe I wasn't clear. I meant, write routines like this:
    >
    > #define SEQUENCE_SIZE 10
    >
    > void srandomtest(unsigned long seed, long sequence[SEQUENCE_SIZE]) {
    > srandom(seed);
    > for(int i = 0; i < SEQUENCE_SIZE; i++)
    > sequence[i] = random();
    > }
    >
    > void srandomdevtest(long sequence[SEQUENCE_SIZE]) {
    > srandomdev()
    > for(int i = 0; i < SEQUENCE_SIZE; i++)
    > sequence[i] = random();
    > }
    >
    > Then use a controlling program like this:
    >
    > void runtest(void) {
    > while(1) {
    > long sequence[SEQUENCE_SIZE];
    > srandomtest(your_seed_generator_here(), sequence);
    > if(already_seen(sequence))
    > printf("srandom() repeated\n");
    > srandomdevtest(sequence)
    > if(already_seen(sequence))
    > printf("srandomdev() repeated\n");
    > }
    > }

    >
    > That wasn't my suggestion. You're right that I didn't try my suggestion,
    > though, because it will probably take several months or years to produce
    > any results if you actually run it.[/ref]

    Yes, you can only start 2**32 different places. But that doesn't affect the
    quality of the sequence of random numbers you get.
    Your original claim, that I objected to, was that seeding with srandomdev
    gives superior random numbers.

    Carsten Hansen


    Carsten Guest

  16. #16

    Default Re: Need help with "Random"

    On Sun, 30 May 2004, Carsten Hansen wrote:
     

    Randomness is just a way of saying that things are hard to predict. If you
    know that you are only starting in one of 2^32 different places, with a
    generator that has a significantly longer period, then your numbers will
    be easier to predict than if you might be starting in any legal state.
    Michael Guest

  17. #17

    Default Re: Need help with "Random"


    "Michael Ash" <com> wrote in message
    news:twistedsys.net... [/ref]
    the [/ref]
    srandomdev 
    >
    > Randomness is just a way of saying that things are hard to predict. If you
    > know that you are only starting in one of 2^32 different places, with a
    > generator that has a significantly longer period, then your numbers will
    > be easier to predict than if you might be starting in any legal state.[/ref]

    Nonsense. There are a multiple of tests for randomness. Do a google search
    on Diehard, or see Knuth Vol. 2, Ch. 3.
    I was asking for test that can differentiate between the two methods of
    seeding.

    random() leaks information with each number. After 62 numbers you have full
    information about the internal state.
    That has nothing to do with the method of seeding.

    Carsten Hansen


    Carsten Guest

  18. #18

    Default Re: Need help with "Random"

    On Sun, 30 May 2004, Carsten Hansen wrote:
     
    >
    > Nonsense. There are a multiple of tests for randomness. Do a google search
    > on Diehard, or see Knuth Vol. 2, Ch. 3.
    > I was asking for test that can differentiate between the two methods of
    > seeding.[/ref]

    It seems we're just talking at cross purposes. The digits of pi are
    random, but known; I doubt if any of your tests could differentiate
    between something based on pi and random()'s output.

    Perhaps I was imprecise when I said that srandomdev() gives better quality
    randomness. It gives a more random seeding. Does that make you happier?
     

    That is interesting, I didn't know that.
    Michael Guest

  19. #19

    Default Re: Need help with "Random"


    "Michael Ash" <com> wrote in message
    news:twistedsys.net... [/ref][/ref]
    you [/ref][/ref]
    a [/ref][/ref]
    will 
    > >
    > > Nonsense. There are a multiple of tests for randomness. Do a google[/ref][/ref]
    search 
    >
    > It seems we're just talking at cross purposes. The digits of pi are
    > random, but known; I doubt if any of your tests could differentiate
    > between something based on pi and random()'s output.
    >
    > Perhaps I was imprecise when I said that srandomdev() gives better quality
    > randomness. It gives a more random seeding. Does that make you happier?
    > [/ref]
    full 
    >
    > That is interesting, I didn't know that.[/ref]

    Well, again you show your lack of knowledge. random() is a Lagged Fibonacci
    RNG with lags of 31 and 28.
    Lagged Fibonacci generators are known to fail the Birthday Spacing Test
    (http://random.com.hr/products/random/Diehard.html).
    Moreover, the family is also known to do poorly on the spectral test (L'
    Ecuyer, P. 1997. Bad lattice structures for vectors of non-successive values
    produced by some linear recurrences. INFORMS Journal on Computing 9 (1):
    57-60).
    E.g. the triples (x_{n},x_{n-3},x_{n+28}) lie in only two planes in the unit
    cube (x_{n} = random()/2**31).

    srandomdev() may give you a more random seeding. But I don't see what
    purpose that serves. random() cannot be used as cryptographically secure
    RNG.

    Carsten Hansen


    Carsten Guest

  20. #20

    Default Re: Need help with "Random"

    Carsten Hansen wrote: 

    well, again you show your abundance of arrogance.

    *PLONK*
    Jhnny Guest

Similar Threads

  1. Replies: 5
    Last Post: February 23rd, 01:24 PM
  2. random "Server did not recognize the value of HTTP Header SOAPAction"
    By Paul Turelinckx in forum ASP.NET Web Services
    Replies: 0
    Last Post: February 9th, 12:26 PM
  3. #26005 [Fbk->Opn]: Random "cannot change the session's ini settings" errors
    By parsnip11 at hotmail dot com in forum PHP Development
    Replies: 0
    Last Post: October 31st, 02:53 PM
  4. #26005 [NEW]: Random "cannot change the session's ini settings" errors
    By parsnip11 at hotmail dot com in forum PHP Development
    Replies: 0
    Last Post: October 27th, 03:53 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139