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 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 ().