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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. random number

random number

Scheduled Pinned Locked Moved C / C++ / MFC
lounge
11 Posts 6 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.
  • S Offline
    S Offline
    shiva shankar
    wrote on last edited by
    #1

    Can someone pls provide me with an algorithim that generates an random number ( 0 to 1000)and it cannot generate the same random number more than once.

    S P 2 Replies Last reply
    0
    • S shiva shankar

      Can someone pls provide me with an algorithim that generates an random number ( 0 to 1000)and it cannot generate the same random number more than once.

      S Offline
      S Offline
      SJolly
      wrote on last edited by
      #2

      The output wouldn't be a random number as you are affecting the outcome, however to generate random numbers: // seed the random number generator using the current time srand((unsigned)time(NULL)); // gets a random number between 0 and 0x7fff int iRandom = rand(); // scale this number down to a value between 0 and 1000. iRandom /= (RAND_MAX / 1000); Regards, Simon

      S 1 Reply Last reply
      0
      • S SJolly

        The output wouldn't be a random number as you are affecting the outcome, however to generate random numbers: // seed the random number generator using the current time srand((unsigned)time(NULL)); // gets a random number between 0 and 0x7fff int iRandom = rand(); // scale this number down to a value between 0 and 1000. iRandom /= (RAND_MAX / 1000); Regards, Simon

        S Offline
        S Offline
        shiva shankar
        wrote on last edited by
        #3

        thanks for your replay Mr.simon but with // seed the random number generator using the current time srand((unsigned)time(NULL)); the number will be repeated, but for me numbers shouldn't repeated in same sequence .

        A S D 3 Replies Last reply
        0
        • S shiva shankar

          thanks for your replay Mr.simon but with // seed the random number generator using the current time srand((unsigned)time(NULL)); the number will be repeated, but for me numbers shouldn't repeated in same sequence .

          A Offline
          A Offline
          Antony M Kancidrowski
          wrote on last edited by
          #4

          You could hold all generated random numbers in a vector and check that the next number has not been used previously. If it has call the random again. As long as you only want a few, i.e small percentage of the possible 1000 pseudo random numbers in your sequence, this will suffice. Ant.

          1 Reply Last reply
          0
          • S shiva shankar

            thanks for your replay Mr.simon but with // seed the random number generator using the current time srand((unsigned)time(NULL)); the number will be repeated, but for me numbers shouldn't repeated in same sequence .

            S Offline
            S Offline
            SJolly
            wrote on last edited by
            #5

            The numbers will be repeated in the same sequence only if you start two copies of the application at the same time - the numbers are psuedorandom. If that isn't good enough then you could seed the timer again each time you get a new random number - then differences such as the processor speed / load etc will mean that the sequence will definitely not be repeated. Regards, Simon

            T 1 Reply Last reply
            0
            • S shiva shankar

              Can someone pls provide me with an algorithim that generates an random number ( 0 to 1000)and it cannot generate the same random number more than once.

              P Offline
              P Offline
              Paul Ranson
              wrote on last edited by
              #6

              class IncInt { private : int i_ ; public : IncInt ( int i ) : i_ ( i ) {} int operator () () { return i_++ ; } } ; void Random1000 () { std::vector vi ; vi.resize ( 1000 ) ; std::generate ( vi.begin (), vi.end (), IncInt ( 0 )) ; std::random_shuffle ( vi.begin (), vi.end ()) ; // do something with randomised sequence... } Gives you vector containing 0-999 in random order. You can use a custom 'random' function as an extra argument to 'random_shuffle' if you wish. I'll leave it up to you to decide how to wrap this up, should it suit your purpose. Paul

              T 1 Reply Last reply
              0
              • S shiva shankar

                thanks for your replay Mr.simon but with // seed the random number generator using the current time srand((unsigned)time(NULL)); the number will be repeated, but for me numbers shouldn't repeated in same sequence .

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                The return value of time() is the number of seconds since January 1, 1970 00:00 (or December 31, 1969 19:00). In any case, as long as its not called two successive times in less than one second, the return value will always be different so srand() will always be seeded uniquely.


                "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                1 Reply Last reply
                0
                • S SJolly

                  The numbers will be repeated in the same sequence only if you start two copies of the application at the same time - the numbers are psuedorandom. If that isn't good enough then you could seed the timer again each time you get a new random number - then differences such as the processor speed / load etc will mean that the sequence will definitely not be repeated. Regards, Simon

                  T Offline
                  T Offline
                  Tim Smith
                  wrote on last edited by
                  #8

                  Seeding the random number generator each time is a VERY BAD IDEA. For example, if you can generate 10,000 random numbers in a second, you will get the same random number each time since time only has a resolution of a second. Also, many random number generators should be initialized with a large prime number. By not doing that, it can take a few interation before the random numbers start getting "good". Reseeding the random number generator constantly places it back in the initial condition of generating a small series of bad values. Tim Smith I'm going to patent thought. I have yet to see any prior art.

                  S 1 Reply Last reply
                  0
                  • P Paul Ranson

                    class IncInt { private : int i_ ; public : IncInt ( int i ) : i_ ( i ) {} int operator () () { return i_++ ; } } ; void Random1000 () { std::vector vi ; vi.resize ( 1000 ) ; std::generate ( vi.begin (), vi.end (), IncInt ( 0 )) ; std::random_shuffle ( vi.begin (), vi.end ()) ; // do something with randomised sequence... } Gives you vector containing 0-999 in random order. You can use a custom 'random' function as an extra argument to 'random_shuffle' if you wish. I'll leave it up to you to decide how to wrap this up, should it suit your purpose. Paul

                    T Offline
                    T Offline
                    Tim Smith
                    wrote on last edited by
                    #9

                    or

                    void Random1000 ()
                    {
                    std::vector vi ;
                    for (int i = 0; i < 1000; i++)
                    vi .push_back (i);
                    std::random_shuffle ( vi.begin (), vi.end ()) ;
                    // do something with randomised sequence...
                    }

                    If you don't want all that STL obfuscation in there. Why write 15 lines of code when 2 works just fine. Tim Smith I'm going to patent thought. I have yet to see any prior art.

                    P 1 Reply Last reply
                    0
                    • T Tim Smith

                      or

                      void Random1000 ()
                      {
                      std::vector vi ;
                      for (int i = 0; i < 1000; i++)
                      vi .push_back (i);
                      std::random_shuffle ( vi.begin (), vi.end ()) ;
                      // do something with randomised sequence...
                      }

                      If you don't want all that STL obfuscation in there. Why write 15 lines of code when 2 works just fine. Tim Smith I'm going to patent thought. I have yet to see any prior art.

                      P Offline
                      P Offline
                      Paul Ranson
                      wrote on last edited by
                      #10

                      I only had to write one line.... But it was necessary to cut and paste a bit to make it make sense here. Anyway it's the ease of using the random_shuffle that's important. The OP didn't want a sequence of random numbers, AFAICT. It could of course be written, void Random1000() { int vi [ 1000 ] ; for ( int i = 0; i < 1000; ++i ) vi [ i ] = i ; std::random_shuffle ( vi, vi + 1000 ) ; ... } Paul

                      1 Reply Last reply
                      0
                      • T Tim Smith

                        Seeding the random number generator each time is a VERY BAD IDEA. For example, if you can generate 10,000 random numbers in a second, you will get the same random number each time since time only has a resolution of a second. Also, many random number generators should be initialized with a large prime number. By not doing that, it can take a few interation before the random numbers start getting "good". Reseeding the random number generator constantly places it back in the initial condition of generating a small series of bad values. Tim Smith I'm going to patent thought. I have yet to see any prior art.

                        S Offline
                        S Offline
                        SJolly
                        wrote on last edited by
                        #11

                        Yes, my bad - I forgot that the time bit was in seconds.

                        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