srand with big numbers

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

    srand with big numbers

    I have read and studied and looked at references and can't find out how
    to do this. Here's where some real knowledge comes in now lets see who in
    clc really knows there stuff ;)

    main(void) {
    srand(time(NULL ));
    printf("%i\\n", rand());
    return 0;

    Simple and forward now here's where I'm stumped. I want the random values
    returned in the above way to be long doubles and the equivalent of
    pow(2,128). But srand takes an unsigned 32 bit on my system and long double
    is 96 bit on my system, or 12 bytes. I can find no conversion functions and
    I don't know if I need casts. Can srand return random values of 2 to the 128
    power? I know this can be done. Someone please show me.

    Thanks


  • Ian Collins

    #2
    Re: srand with big numbers

    Bill Cunningham wrote:
    I have read and studied and looked at references and can't find out how
    to do this. Here's where some real knowledge comes in now lets see who in
    clc really knows there stuff ;)
    >
    main(void) {
    srand(time(NULL ));
    printf("%i\\n", rand());
    return 0;
    >
    Simple and forward now here's where I'm stumped. I want the random values
    returned in the above way to be long doubles and the equivalent of
    pow(2,128). But srand takes an unsigned 32 bit on my system and long double
    is 96 bit on my system, or 12 bytes. I can find no conversion functions and
    I don't know if I need casts. Can srand return random values of 2 to the 128
    power? I know this can be done. Someone please show me.
    >
    srand() seeds a pseudo-random number generator, it does not return anything.

    rand() an int the range of 0 to RAND_MAX. RAND_MAX is only guaranteed
    to be 32767.

    You will have to use a platform extension to get bigger random numbers.

    --
    Ian Collins.

    Comment

    • Bill Cunningham

      #3
      Re: srand with big numbers

      You will have to use a platform extension to get bigger random numbers.
      >
      OK anything you might suggest?

      Bill


      Comment

      • Ian Collins

        #4
        Re: srand with big numbers

        Bill Cunningham wrote:
        >You will have to use a platform extension to get bigger random numbers.
        >>
        OK anything you might suggest?
        >
        The drand48 family if you are using a Unix/Linux platform.

        Try a platform specific programming group.

        --
        Ian Collins.

        Comment

        • Sjouke Burry

          #5
          Re: srand with big numbers

          Bill Cunningham wrote:
          I have read and studied and looked at references and can't find out how
          to do this. Here's where some real knowledge comes in now lets see who in
          clc really knows there stuff ;)
          >
          main(void) {
          srand(time(NULL ));
          printf("%i\\n", rand());
          return 0;
          >
          Simple and forward now here's where I'm stumped. I want the random values
          returned in the above way to be long doubles and the equivalent of
          pow(2,128). But srand takes an unsigned 32 bit on my system and long double
          is 96 bit on my system, or 12 bytes. I can find no conversion functions and
          I don't know if I need casts. Can srand return random values of 2 to the 128
          power? I know this can be done. Someone please show me.
          >
          Thanks
          >
          >
          Google : long period random generator

          Comment

          • vippstar@gmail.com

            #6
            Re: srand with big numbers

            On Apr 1, 3:58 am, Ian Collins <ian-n...@hotmail.co mwrote:
            Bill Cunningham wrote:
            I have read and studied and looked at references and can't find out how
            to do this. Here's where some real knowledge comes in now lets see who in
            clc really knows there stuff ;)
            >
            main(void) {
            forgot int, but surprisingly not 'void'.
            srand(time(NULL ));
            no prototypes in scope
            printf("%i\\n", rand());
            it's not guaranteed to be written to stdout (ie no flush performed)
            because instead of \n you had \\n, missing prototype of printf
            return 0;
            forgot } here
            >
            Simple and forward now here's where I'm stumped I want the random values
            rand() is not truly random
            returned in the above way to be long doubles and the equivalent of
            pow(2,128).
            ambiguous
            But srand takes an unsigned 32 bit on my system and long double
            is 96 bit on my system, or 12 bytes.
            srand() ha nothing to do with the size of what rand() returns, so,
            nonsense.
            I can find no conversion functions and
            Now baiting someone to explain him what a "conversion " is or what he
            *might* mean with it
            I don't know if I need casts. Can srand return random values of 2 to the 128
            power? I know this can be done. Someone please show me.
            First he asks then he claims he knows it can be done.
            Baiting someone to post a long rant about *why* it cannot be done

            conclusion: TROLL.
            Everything I commented at is things regulars could argue for hours
            with huge posts. A waste of time for everyone.
            It's clear Bill Cunningham is a troll! Why cannot the community ignore
            him like the other trolls?
            >
            srand() seeds a pseudo-random number generator, it does not return anything.
            >
            rand() an int the range of 0 to RAND_MAX. RAND_MAX is only guaranteed
            to be 32767.
            At *least* 32767, it could be 2^31-1 for example (which is the case on
            my implementation) .
            Don't reply to trolls.

            Comment

            • Bill Cunningham

              #7
              Re: srand with big numbers

              Google : long period random generator

              ;) You got it. I am trying to learn by creating a UUID generator. I know I
              can get one but how do you learn from that. Thanks

              Bill


              Comment

              • Bill Cunningham

                #8
                Re: srand with big numbers


                "Ian Collins" <ian-news@hotmail.co mwrote in message
                news:65dg3iF2e7 qncU7@mid.indiv idual.net...
                The drand48 family if you are using a Unix/Linux platform.
                >
                Try a platform specific programming group.
                >
                This is probably written in C so C can do this. It's just in the std
                library. THanks Ian. I'm seeing erand and jrand too.

                Bill


                Comment

                • Bartc

                  #9
                  Re: srand with big numbers


                  "Ian Collins" <ian-news@hotmail.co mwrote in message
                  news:65dfkoF2e7 qncU5@mid.indiv idual.net...
                  Bill Cunningham wrote:
                  > I have read and studied and looked at references and can't find out
                  >how
                  >to do this. Here's where some real knowledge comes in now lets see who in
                  >clc really knows there stuff ;)
                  srand() seeds a pseudo-random number generator, it does not return
                  anything.
                  >
                  rand() an int the range of 0 to RAND_MAX. RAND_MAX is only guaranteed
                  to be 32767.
                  >
                  You will have to use a platform extension to get bigger random numbers.
                  I just use something like this:

                  #define bigrand (rand()<<16 + rand())

                  Any particular problem with that? The randomness is not critical.

                  --
                  Bart


                  Comment

                  • Philip Potter

                    #10
                    Re: srand with big numbers

                    Bartc wrote:
                    >rand() an int the range of 0 to RAND_MAX. RAND_MAX is only guaranteed
                    >to be 32767.
                    >>
                    >You will have to use a platform extension to get bigger random numbers.
                    >
                    I just use something like this:
                    >
                    #define bigrand (rand()<<16 + rand())
                    >
                    Any particular problem with that? The randomness is not critical.
                    Suppose for a moment that the period of rand() is 65536. The period of
                    your aggregate generator bigrand will be 32768 - that is, 32768 samples
                    after the first random sample, you will get the same sequence out again.
                    Aggregating the outputs together cannot increase the period.

                    If the randomness is not critical, then perhaps that's good enough for
                    you. I can't answer that for you.

                    Philip

                    Comment

                    • Ben Bacarisse

                      #11
                      Re: srand with big numbers

                      "Bartc" <bc@freeuk.comw rites:
                      "Ian Collins" <ian-news@hotmail.co mwrote in message
                      news:65dfkoF2e7 qncU5@mid.indiv idual.net...
                      >Bill Cunningham wrote:
                      >> I have read and studied and looked at references and can't find out
                      >>how
                      >>to do this. Here's where some real knowledge comes in now lets see who in
                      >>clc really knows there stuff ;)
                      >
                      >srand() seeds a pseudo-random number generator, it does not return
                      >anything.
                      >>
                      >rand() an int the range of 0 to RAND_MAX. RAND_MAX is only guaranteed
                      >to be 32767.
                      >>
                      >You will have to use a platform extension to get bigger random numbers.
                      >
                      I just use something like this:
                      >
                      #define bigrand (rand()<<16 + rand())
                      >
                      Any particular problem with that? The randomness is not critical.
                      Yes. You probably meant ((rand()<<16) + rand()). Frequent UB otherwise.

                      As for the crrected version... tt depends. If RAND_MAX is only 32767
                      you get two "dead bits" in the result. Worse, if the system has 16
                      bit ints the shift is undefined. (unsigned long)rand() << 16 is safer.

                      --
                      Ben.

                      Comment

                      • Bill Cunningham

                        #12
                        Re: srand with big numbers

                        Yes. You probably meant ((rand()<<16) + rand()). Frequent UB otherwise.
                        >
                        As for the crrected version... tt depends. If RAND_MAX is only 32767
                        you get two "dead bits" in the result. Worse, if the system has 16
                        bit ints the shift is undefined. (unsigned long)rand() << 16 is safer.
                        >
                        --
                        Is this an example of what's called a "macro expansion" in C ? I haven't
                        got there yet but I will try this.

                        Bill


                        Comment

                        • CBFalconer

                          #13
                          Re: srand with big numbers

                          Bill Cunningham wrote:
                          >
                          I have read and studied and looked at references and can't find
                          out how to do this. Here's where some real knowledge comes in now
                          lets see who in clc really knows there stuff ;)
                          >
                          main(void) {
                          srand(time(NULL ));
                          printf("%i\\n", rand());
                          return 0;
                          >
                          Simple and forward now here's where I'm stumped. I want the random
                          values returned in the above way to be long doubles and the
                          equivalent of pow(2,128). But srand takes an unsigned 32 bit on my
                          system and long double is 96 bit on my system, or 12 bytes. I can
                          find no conversion functions and I don't know if I need casts. Can
                          srand return random values of 2 to the 128 power? I know this can
                          be done. Someone please show me.
                          7.20.2.1 The rand function

                          Synopsis
                          [#1]
                          #include <stdlib.h>
                          int rand(void);

                          .... snip ...

                          Returns

                          [#4] The rand function returns a pseudo-random integer.

                          Environmental limits

                          [#5] The value of the RAND_MAX macro shall be at least
                          32767.

                          7.20.2.2 The srand function

                          Synopsis
                          [#1]
                          #include <stdlib.h>
                          void srand(unsigned int seed);

                          Description

                          [#2] The srand function uses the argument as a seed for a
                          new sequence of pseudo-random numbers to be returned by
                          subsequent calls to rand. If srand is then called with the
                          same seed value, the sequence of pseudo-random numbers shall
                          be repeated. If rand is called before any calls to srand
                          have been made, the same sequence shall be generated as when
                          srand is first called with a seed value of 1.

                          [#3] The implementation shall behave as if no library
                          function calls the srand function.

                          Returns

                          [#4] The srand function returns no value.

                          --
                          [mail]: Chuck F (cbfalconer at maineline dot net)
                          [page]: <http://cbfalconer.home .att.net>
                          Try the download section.



                          --
                          Posted via a free Usenet account from http://www.teranews.com

                          Comment

                          Working...