Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Help with rand() definition [modified]

Help with rand() definition [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestionlounge
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Grimes
    wrote on last edited by
    #1

    Hello everybody I am looking for the rand() function definition, because I want to use it to create a similar function for a super secret project of mine :suss:. I have found a website on the Internet that claims the rand() function definition is something like:

    unsigned long int next = 1;
    int rand()
    {
    next = next * 1103515245 + 12345;
    return (unsigned int)(next/65536) % 32768;
    }

    however if I use this function it does not give me the same value as the rand() function.

    cout << (rand() %50) << "\n";
    cout << (rand() %50) << "\n";

    gives the first random value of 41 and second value of 17. (the srand() function was not used so these are the same every time I run the program) and when I use the definition I got from the website

    long unsigned int Getal = 1;
    Getal = (Getal*1103515245 + 12345);
    cout << (Getal/65536) % 32768 % 50 << "\n";
    Getal = (Getal*1103515245 + 12345);
    cout << (Getal/65536) % 32768 % 50 << "\n";

    the first output is 38 and the second 8. Maybe there is something wrong with my implementation, or can someone maybe help or tell me what the correct definition is. I am using Visual C++ 6 and use the rand() function found in the stdlib.h. Is the data types I used correct? Where can I find the definition in the stdlib.h? I tried looking there but there's only a line that says "_CRTIMP int __cdecl rand(void);" ?!? Anyway I would appreciate help thanks grimes

    KOM UIT DAAAAA!!!

    modified on Friday, June 12, 2009 1:04 PM

    L L 3 Replies Last reply
    0
    • G Grimes

      Hello everybody I am looking for the rand() function definition, because I want to use it to create a similar function for a super secret project of mine :suss:. I have found a website on the Internet that claims the rand() function definition is something like:

      unsigned long int next = 1;
      int rand()
      {
      next = next * 1103515245 + 12345;
      return (unsigned int)(next/65536) % 32768;
      }

      however if I use this function it does not give me the same value as the rand() function.

      cout << (rand() %50) << "\n";
      cout << (rand() %50) << "\n";

      gives the first random value of 41 and second value of 17. (the srand() function was not used so these are the same every time I run the program) and when I use the definition I got from the website

      long unsigned int Getal = 1;
      Getal = (Getal*1103515245 + 12345);
      cout << (Getal/65536) % 32768 % 50 << "\n";
      Getal = (Getal*1103515245 + 12345);
      cout << (Getal/65536) % 32768 % 50 << "\n";

      the first output is 38 and the second 8. Maybe there is something wrong with my implementation, or can someone maybe help or tell me what the correct definition is. I am using Visual C++ 6 and use the rand() function found in the stdlib.h. Is the data types I used correct? Where can I find the definition in the stdlib.h? I tried looking there but there's only a line that says "_CRTIMP int __cdecl rand(void);" ?!? Anyway I would appreciate help thanks grimes

      KOM UIT DAAAAA!!!

      modified on Friday, June 12, 2009 1:04 PM

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, I could come up with an infinite number of pseudo-random-number-generators; they all are periodic, i.e. after N calls they repeat the sequence; you would hope N to be large. The simple ones have a single state variable (next in your snippet) that is operated upon (e.g. with multiply and add; or with shift and exor) and result in a period of at most 2^B for a state variable holding B bits. I can't tell you which one is used in the standard libraries, maybe because that is super secret too. I suggest you Google rand.c to get a small collection. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


      G 1 Reply Last reply
      0
      • G Grimes

        Hello everybody I am looking for the rand() function definition, because I want to use it to create a similar function for a super secret project of mine :suss:. I have found a website on the Internet that claims the rand() function definition is something like:

        unsigned long int next = 1;
        int rand()
        {
        next = next * 1103515245 + 12345;
        return (unsigned int)(next/65536) % 32768;
        }

        however if I use this function it does not give me the same value as the rand() function.

        cout << (rand() %50) << "\n";
        cout << (rand() %50) << "\n";

        gives the first random value of 41 and second value of 17. (the srand() function was not used so these are the same every time I run the program) and when I use the definition I got from the website

        long unsigned int Getal = 1;
        Getal = (Getal*1103515245 + 12345);
        cout << (Getal/65536) % 32768 % 50 << "\n";
        Getal = (Getal*1103515245 + 12345);
        cout << (Getal/65536) % 32768 % 50 << "\n";

        the first output is 38 and the second 8. Maybe there is something wrong with my implementation, or can someone maybe help or tell me what the correct definition is. I am using Visual C++ 6 and use the rand() function found in the stdlib.h. Is the data types I used correct? Where can I find the definition in the stdlib.h? I tried looking there but there's only a line that says "_CRTIMP int __cdecl rand(void);" ?!? Anyway I would appreciate help thanks grimes

        KOM UIT DAAAAA!!!

        modified on Friday, June 12, 2009 1:04 PM

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Grimes wrote:

        I am looking for the rand() function definition

        You failed to mention what compiler you are using. Your in luck, I have several installed. Visual Studio 6: C:\Program Files (x86)\Microsoft Visual Studio\VC98\CRT\SRC\RAND.C Visual Studio 2005: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\crt\src\rand.c Visual Studio 2008: C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\crt\src\rand.c You could also take a look at the rand implementation in the GNU LibC library[^]. The rand functions are located in the \glibc-XX\stdlib folder. Best Wishes, -David Delaune

        G 1 Reply Last reply
        0
        • L Luc Pattyn

          Hi, I could come up with an infinite number of pseudo-random-number-generators; they all are periodic, i.e. after N calls they repeat the sequence; you would hope N to be large. The simple ones have a single state variable (next in your snippet) that is operated upon (e.g. with multiply and add; or with shift and exor) and result in a period of at most 2^B for a state variable holding B bits. I can't tell you which one is used in the standard libraries, maybe because that is super secret too. I suggest you Google rand.c to get a small collection. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


          G Offline
          G Offline
          Grimes
          wrote on last edited by
          #4

          The reason I said it is super secret is so that when the CIA/FBI/NSA kick down your door to arrest you as one of my accomplices, you can have deniability. I thought that there was a simple random function that most programmers, that are too lazy to make their own, use. Perhaps you know of a method to predict the next number of a randomizer function. This is why I wanted to know the definition of the rand() function so I can write a program to predict the next 'random' number in a series. but thanks anyway... by the way, after this conversation, your phone might be tapped...

          KOM UIT DAAAAA!!!

          1 Reply Last reply
          0
          • L Lost User

            Grimes wrote:

            I am looking for the rand() function definition

            You failed to mention what compiler you are using. Your in luck, I have several installed. Visual Studio 6: C:\Program Files (x86)\Microsoft Visual Studio\VC98\CRT\SRC\RAND.C Visual Studio 2005: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\crt\src\rand.c Visual Studio 2008: C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\crt\src\rand.c You could also take a look at the rand implementation in the GNU LibC library[^]. The rand functions are located in the \glibc-XX\stdlib folder. Best Wishes, -David Delaune

            G Offline
            G Offline
            Grimes
            wrote on last edited by
            #5

            OK thank you. I have Visual Studio 6, and I found the elusive 'rand.c' file I will take a look at it, thanks for the help!

            KOM UIT DAAAAA!!!

            1 Reply Last reply
            0
            • G Grimes

              Hello everybody I am looking for the rand() function definition, because I want to use it to create a similar function for a super secret project of mine :suss:. I have found a website on the Internet that claims the rand() function definition is something like:

              unsigned long int next = 1;
              int rand()
              {
              next = next * 1103515245 + 12345;
              return (unsigned int)(next/65536) % 32768;
              }

              however if I use this function it does not give me the same value as the rand() function.

              cout << (rand() %50) << "\n";
              cout << (rand() %50) << "\n";

              gives the first random value of 41 and second value of 17. (the srand() function was not used so these are the same every time I run the program) and when I use the definition I got from the website

              long unsigned int Getal = 1;
              Getal = (Getal*1103515245 + 12345);
              cout << (Getal/65536) % 32768 % 50 << "\n";
              Getal = (Getal*1103515245 + 12345);
              cout << (Getal/65536) % 32768 % 50 << "\n";

              the first output is 38 and the second 8. Maybe there is something wrong with my implementation, or can someone maybe help or tell me what the correct definition is. I am using Visual C++ 6 and use the rand() function found in the stdlib.h. Is the data types I used correct? Where can I find the definition in the stdlib.h? I tried looking there but there's only a line that says "_CRTIMP int __cdecl rand(void);" ?!? Anyway I would appreciate help thanks grimes

              KOM UIT DAAAAA!!!

              modified on Friday, June 12, 2009 1:04 PM

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              If you use % (as in rand() % 50) on a random number, the distribution of its values will change - by a lot. I know that's not what you asked, so this isn't an answer, and for many uses of rand it doesn't even matter.. but it's something to keep in mind. Oh and % (1< can be replaced by `` & ((1< which is a lot faster - you probably knew that (everyone does) but I saw a `% 32768` ``

              G 1 Reply Last reply
              0
              • L Lost User

                If you use % (as in rand() % 50) on a random number, the distribution of its values will change - by a lot. I know that's not what you asked, so this isn't an answer, and for many uses of rand it doesn't even matter.. but it's something to keep in mind. Oh and % (1< can be replaced by `` & ((1< which is a lot faster - you probably knew that (everyone does) but I saw a `% 32768` ``

                G Offline
                G Offline
                Grimes
                wrote on last edited by
                #7

                I know that there will be many values of next for every possible random number. I calculated that for the random generator I have, that there will be almost 1311 values of 'next' for every random number. I got this by: (32768/50)*(65536/32768)=65536/50 = 1311. I was hoping to predict the next random number in a series by using these 1311 values to see which of those values would give the next random number. I order to do this I need the definition of the random function used and I also need x amount of previous random numbers in order to predict the next. It seems from everybodie's replies that there are many different random number generators, I was hoping that there was a common function used by most programmers. but thanks anyway...

                KOM UIT DAAAAA!!!

                T 1 Reply Last reply
                0
                • G Grimes

                  I know that there will be many values of next for every possible random number. I calculated that for the random generator I have, that there will be almost 1311 values of 'next' for every random number. I got this by: (32768/50)*(65536/32768)=65536/50 = 1311. I was hoping to predict the next random number in a series by using these 1311 values to see which of those values would give the next random number. I order to do this I need the definition of the random function used and I also need x amount of previous random numbers in order to predict the next. It seems from everybodie's replies that there are many different random number generators, I was hoping that there was a common function used by most programmers. but thanks anyway...

                  KOM UIT DAAAAA!!!

                  T Offline
                  T Offline
                  T2102
                  wrote on last edited by
                  #8

                  Are you using this for encryption? If not, you might leverage mersenne twister's random //generates a random number on [0,0xffffffff]-interval The MT has a period of at least 2^19937 - 1 back a few years ago and is probably larger now. Mersenne Twister is basically for Monte-Carlo simulations - it is not cryptographically secure "as is". http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html[^] Are simply trying to set the seed on the rand() function or learn how to crack linear congruential generators?

                  modified on Saturday, June 13, 2009 5:03 AM

                  G 1 Reply Last reply
                  0
                  • T T2102

                    Are you using this for encryption? If not, you might leverage mersenne twister's random //generates a random number on [0,0xffffffff]-interval The MT has a period of at least 2^19937 - 1 back a few years ago and is probably larger now. Mersenne Twister is basically for Monte-Carlo simulations - it is not cryptographically secure "as is". http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html[^] Are simply trying to set the seed on the rand() function or learn how to crack linear congruential generators?

                    modified on Saturday, June 13, 2009 5:03 AM

                    G Offline
                    G Offline
                    Grimes
                    wrote on last edited by
                    #9

                    This helps a LOT! I was looking for a number generator that can be so 'random' as this mersenne twister. I am looking at methods for cracking random number generators, do you know of any good source of literature on this subject? I have googled it, but don't know which info is good, and which is junk. I am also wondering how hard is it to crack a cryptographically secure number generator? Is it impossible or is it so difficult that it is impractical to even try. I mean, will you need thousands/millions or billions of iteration values before you can start to predict the next iteration? thanks

                    KOM UIT DAAAAA!!!

                    T 1 Reply Last reply
                    0
                    • G Grimes

                      This helps a LOT! I was looking for a number generator that can be so 'random' as this mersenne twister. I am looking at methods for cracking random number generators, do you know of any good source of literature on this subject? I have googled it, but don't know which info is good, and which is junk. I am also wondering how hard is it to crack a cryptographically secure number generator? Is it impossible or is it so difficult that it is impractical to even try. I mean, will you need thousands/millions or billions of iteration values before you can start to predict the next iteration? thanks

                      KOM UIT DAAAAA!!!

                      T Offline
                      T Offline
                      T2102
                      wrote on last edited by
                      #10

                      DES is not that secure for example. Web browser using 256 bit DES could be cracked in several hours using a cluster. When I was in college ten years ago, I read a book by Douglas R. Stinson; the latest version is http://www.amazon.com/Cryptography-Practice-Discrete-Mathematics-Applications/dp/1584885084/ref=sr_1_1?ie=UTF8&s=books&qid=1244886755&sr=8-1[^] You also will want to read about why Mersenne Twister is not secure; I am sure it's discussed on that project webpage or another site. The math is mainly Modern Algebra like Chinese Remainder Theorem and basic statistics.

                      modified on Saturday, June 13, 2009 6:11 AM

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups