#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <numeric>
int main()
{
const std::size_t SZ = 80 ;
const std::size_t N = 20 ;
int KenoArray[SZ] ;
std::iota( KenoArray, KenoArray+SZ, 1 ) ; // fill with numbers 1 2 ... SZ
std::srand( std::time(0) ) ; // seed the rng
// randomly shuffle elements in the array
std::random_shuffle( KenoArray, KenoArray+SZ ) ;
// pick the first N elements in the array for N unique random numbers in [1,SZ]
for( std::size_t i = 0 ; i < N ; ++i ) std::cout << KenoArray[i] << ' ' ;
std::cout << '\n' ;
}
I tried putting count++ in the if didn't work... still getting dupes.
JLBorges I haven't learned what you wrote.. anyway to help me with what I have already? Also it needs to be in VS2012
#include <iostream>
#include <cstdlib>
#include <ctime>
int main()
{
const std::size_t SZ = 81 ;
const std::size_t N = 20 ;
if( N > (SZ-5) ) return 1 ; // sanity check to avoid unbounded loop
std::srand( std::time(0) ) ; // seed the rng
int KenoArray[SZ] = { 0 }; // initialise with zeroes
std::size_t cnt = 0 ; // count of unique random numbers generated
while( cnt < N ) // till N unique random numbers have been generated
{
constint n = std::rand() % (SZ-1) + 1 ; // candidate number
if( KenoArray[n] == 0 ) // if not already generated
{
KenoArray[n] = n ; // set the value in the array
++cnt ; // increment count of generated numbers
std::cout << cnt << ". unique random number " << n << '\n' ; // for testing
}
}
}
here. you print regardless of dupes or not, its before you checked!
you count whether it duped or not, that was after the check.
the logic should be, get random, if not a dupe, count it and process (in this case, print) it.
Ill admit the above modern c++ is probably better, but Ill work around what you have to explain the problem. A total rewrite does not show your bugs. Study both, learn the modern way above, but understand your mistakes also.