I'm fairly new to c++ and I'm making a random number generator program.
Update:
I've already done the generator part but the program requires that the random numbers must never repeat. I'm not sure how to do that without arrays so i would prefer simple looping if possible. Any help is greatly appreciated! Here's what I have so far:
the random numbers must never repeat ... without arrays
If you don't store what you have already got, then you cannot know what you have already got. If you don't remember the past, then I can give you 7 every single time and you will know no repeat.
Where does the "without arrays" come from and what is its exact formulation?
First I'm going to argue that you can not do that without restrictions.
If you have a random number from 1-100 and you create 101 random more numbers, something is going to repeat, and your program will probably crash if it tries to prevent it, so you have to have some kind of restriction.
There are several silly ways to do this so easy it will seem like magic...
Create a random number from 1-10, then 11-20, 21-30 and so on...
or
Create a random number that ends in 1, another that ends in 2-9....
or
Create the numbers, sort them and remove any duplicates.
or
Create the first number and always make sure the next number is smaller or larger.
You may argue this is not truly random, but what your doing to prevent any number from reappearing is also not truly random....
1. You remember each value that you generate. If you happen to generate a value that has already been generated you just repeat until you get a value that has not yet been generated. This often works great when you just want to generate a small number of values from a much larger range of possible values. It can be terribly slow though if there are very few possible numbers left because it gets more likely that it will have to loop many times before finding a value that has not yet been generated.
2. You generate all possible values up-front and just pick one at a time. You can either randomize all numbers from the start and then pick one after each other in order, or you can just leave all numbers in order and then pick (and remove) a value at random each time. This does not have the problem of degrading performance but if the range of possible numbers is large it will require a lot of memory.
You don't need the clearVector() method. Just call results.clear() at line 32.
The way you check for duplicates is very inefficient. I'd create an array of 101 numbers, initialize it to zero and then if the RNG gives K, set array[K]. You might find that using a bitset<101> is faster. It will initialize faster, but accessing it takes a little more time.
Personally, I'd rearrange the code somewhat. I'd make the class generate random numbers from 1 to100 without repeats. That's all it would do. I'd move the code that interacts with the user and counts odd/even numbers into main.