Help picking 20 random numbers without repeats

this is my code... I am using vs 2012. I need to pick 20 random numbers from a generator without repeats

srand(time(0));

int KenoArray[81]= {0};

int j;
int count= 0;
int ranNum;

count=0;
while(count<20)
{
ranNum= rand() % 80+ 1;
cout<< ranNum<< endl;

if (KenoArray[ranNum] == 0)
{
KenoArray[ranNum] = ranNum;

}
count++;
}


Im not sure what im doing wrong. Thanks
it looks like it needs
count++; inside the if. Outside, if you get a repeat, it still increments.



Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iomanip>

int main()
{
    const std::size_t SZ = 81 ;
    const std::size_t N = 20 ;

    int KenoArray[SZ] = { 0 } ; // initialise to all zeroes
    std::fill_n( KenoArray+1, N, 1 ) ;  // fill KenoArray[1] ... KenoArray[N] with ones

    std::srand( std::time(0) ) ; // seed the rng

    // randomly shuffle elements in positions 1 .. SZ
    // note: deprecated and removed, consider upgrading to Visual Studio 2017 Community
    // http://en.cppreference.com/w/cpp/algorithm/random_shuffle
    std::random_shuffle( KenoArray+1, KenoArray+SZ ) ;

    // iterate through the array, update values of the randomly selected positions
    // note: selected positions contain one, the rest are zeroes
    for( std::size_t n = 0 ; n < SZ ; ++n ) KenoArray[n] *= n ; // 1*n == n; 0*n == 0
}
Last edited on
If all that is required in 20 unique random numbers in [1,80]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#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' ;
}

http://coliru.stacked-crooked.com/a/5d1b7734372690c2
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
OP: google gave this link among others http://www.cplusplus.com/forum/general/7784/#msg36171
search by: "random numbers without duplicates c++"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#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
    {
        const int 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.

srand(time(0));

int KenoArray[81]= {0};

int j;
int count= 0;
int ranNum;

count=0;
while(count<20)
{
ranNum= rand() % 80+ 1;

if (KenoArray[ranNum] == 0)
{
KenoArray[ranNum] = ranNum;
cout<< ranNum<< endl;
count++;
}
}

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.

Last edited on
Oh that makes sense... I knew i had the correct things i just put them in the wrong places. Thanks!
Topic archived. No new replies allowed.