non-repeating numbers in generating numbers

The codes do at it is(generating numbers) but when it generates, the number is repeating. How to solve it?

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<iostream>
#include<cstdlib>               //for using rand
#include<ctime>                 //to include time in seconds,
                                //every time the specific number is inputed it will give random numbers
void RandomGenerator(int lmt);      //function declaration

using namespace std;

int main()
{
    int limit;

    cout<< "Enter a number that is equal or greater than two: \n"<<endl;
    while(!(cin>>limit) || limit<2)
    {
        cout<< "Invalid number! It must be greater than 2. Please try again: \n"<<endl;
        system("PAUSE");
        system("CLS");

        cin.clear();
        cin.ignore(10000, '\n');
        cout<< "Enter a number that is equal or greater than two: \n"<<endl;
    }
    cout<<endl;

    cout<< "The random numbers between "<<limit<< " is: \n"<<endl;
    RandomGenerator(limit);         //function calling
    cout<<endl;

    return 0;
}

void RandomGenerator(int lmt)       //function definition
{
    srand(time(0));

    for(int v=0; v<lmt; v++)
    {
        cout<< (rand()%lmt)<< "  ";
    }
}



Enter a number that is equal or greater than two:

5

1  4  1  3  3
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
26
27
28
#include <iostream>
#include <random>
#include <vector>
#include <algorithm>

void print_random_sequence( int lmt )
{
    // http://en.cppreference.com/w/cpp/numeric/random
    static std::mt19937 rng( std::random_device{}() ) ;

    // create a sequence 0, 1, 2, .... lmt-1
    std::vector<int> seq ; // https://cal-linux.com/tutorials/vectors.html
    for( int n = 0 ; n < lmt ; ++n ) seq.push_back(n) ;

    // shuffle the sequence to get a random ordering of the elements
    // http://en.cppreference.com/w/cpp/algorithm/random_shuffle
    std::shuffle( seq.begin(), seq.end(), rng ) ;

    // print the randomly ordered sequence
    // http://www.stroustrup.com/C++11FAQ.html#for
    for( int n : seq ) std::cout << n << ' ' ;
    std::cout << '\n' ;
}

int main()
{
    print_random_sequence(28) ;
}

http://coliru.stacked-crooked.com/a/db75c0fa958a8e35
Topic archived. No new replies allowed.