Why make the rand with new every time so difficult?

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Why make the rand with new every time so difficult?

      earlier today in chapter 3 I read about when he was going to do a system that returns a number within the size of the array, but with new every time.
      the part of the chapter was called "Pseudo-Random Traversal of a set"

      basically, the Mikey used prime numbers to make it possible. but why make the code so extremely advanced? here is what I think would give same results without having prime numbers, and mutch easier to make it a big size

      I know how to put everything down in real code but I'm to lazy for it, hope you understands anyway

      class theClass
      {
      int* array;
      int currentsize;

      }
      Contructor(int size) // how big the size you will use will be
      {
      array = new int[size];
      currentsize = size;
      for ( i=0 ; i< size; ++i)
      array = i;
      }

      int Next(bool restart)
      {
      int where = rand() % currentsize;
      swap(array[where], array[currentsize-1]);
      currentsize--;
      return where;
      }

      of course you have to do all the checks also, but when you do it like this you get much less code, and much easier to understand also, and when you want to restart, you just change the currentsize. It doesnt matter that the numbers isnt in order since it is only rand you will use them for.

      Feel free to comment

      The post was edited 1 time, last by Ralgud ().

    • RE: Why make the rand with new every time so difficult?

      I think the difference in the more complicated solution is mine is guaranteed to traverse the set exactly once, with no overwrites. That's why it takes a little more code...
      Mr.Mike
      Author, Programmer, Brewer, Patriot
    • Ok, do you mean the override the swap function does or is it any more place?
      If there is any other way I would gladly to know where it can occure.

      And is it any problem if it's only doing it with the swap. I mean swap doesnt really take any time, and if it swap with itself no harm is done. because that just means it is the one that are going to be removed from the active list anyway

      The post was edited 1 time, last by Ralgud ().

    • Oh I see - yes your solution is easier and it will work - but it also requires more memory, for your swap array.

      One of the applications of my random traversal of the array is to black out a screen - which could be 1600x1200 pixels - so that's a big extra memory allocation!
      Mr.Mike
      Author, Programmer, Brewer, Patriot
    • The standard rand() function has in general not a very good probability distribution, so it is recommended to use a more sophisticated way to produce your randomized data in most cases.

      But you don't have to write it all yourself you can just use the random number generator framework from boost.



      btw. Mr. Mike will you stop in the 3rd Edition of writing so many classes yourself although they're already in the boost framework, which you're using anyway? I think your book is less about implementing a good random number generator and others (I think you've written your own smart pointer class, too but on that I'm not so sure).
    • I used boost in the 2nd edition - maybe you are talking about the 1st edition???

      And, I didn't write a rand() - I used a very famous one that has a great distribution.
      Mr.Mike
      Author, Programmer, Brewer, Patriot
    • Originally posted by mrmike
      I used boost in the 2nd edition - maybe you are talking about the 1st edition???

      And, I didn't write a rand() - I used a very famous one that has a great distribution.

      Hi,

      I just looked into the sources and I think you're right. Because I've read the 1st and 2ns edition and both was a while ago I must have confused this.


      If I remeber correctly you implemented the mersenne twister yourself (I couldn't finde the source file containing it), but there you could also use the boost.Random framework which gives you a very flexible control about the number generation, for a quick introduction you can look at the demo-source.