Generate 8 unique random numbers from 0 to 7

2020-06-29 02:45发布

I'm making a simple script that generates 8 random values from 0 to 7 and stores them into an array named random_numbers.

This is my try:

int main(int argc, char** argv) {

    int random_numbers[8];
    srand((unsigned)time(NULL));
    for (int i = 0; i < 8; i++) {
        random_numbers[i] = 1+ rand() % 8;
        cout << random_numbers[i] << endl;
    }
    return 0;
}

This gives me repeated values. I would like to have random_numbers filled of random values from 0 to 7, but without any repeated numbers.

How can I do that?

4条回答
我只想做你的唯一
2楼-- · 2020-06-29 02:58

Since you are using c++ any way I modified you code a bit more. Use vector instead of array, this should make your life easier, and this generates 0-7 in a random order without duplicates:

compile with -std=c++0x

#include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>

using namespace std;

int main(int argc, char** argv) {

    srand(time(NULL));
    vector<int> random_numbers(8);
    iota(random_numbers.begin(), random_numbers.end(), 1);

    random_shuffle(random_numbers.begin(), random_numbers.end());
    for (unsigned i = 0; i < random_numbers.size(); i++) {
        cout << random_numbers[i] << endl;
    }
    return 0;
}
查看更多
够拽才男人
3楼-- · 2020-06-29 03:02

try this code:

int random_numbers[8];

int main(int argc, char** argv) {

        srand((unsigned)time(NULL));
        for (int i = 0; i < 8; i++) {
                random_numbers[i] =getRand(i);
                cout << random_numbers[i] << endl;
        }
    return 0;
}
int getRand(int entries)
{
    int flag = 0;
    int randno ;
    randno = rand() % 8;
    for(int i=0;i<entries;i++)
    {

        if( random_numbers[i]==randno)
        {
           randno = rand() % 8;
           continue;
        }
    }
    return randno;

}
查看更多
ゆ 、 Hurt°
4楼-- · 2020-06-29 03:06

Generate the numbers 0 through 7, then shuffle them. Assuming this is actually C++, not C (because of the cout), use std::random_shuffle:

#include <algorithm>
#include <cstdlib>
#include <iostream>

int main()
{
    int a[8] = {0, 1, 2, 3, 4, 5, 6, 7};
    std::srand(unsigned(std::time(0)));

    std::random_shuffle(a, a + 8);

    for (int i=0; i<8; i++)
        std::cout << a[i] << " ";
    std::cout << std::endl;
}

Be warned: cppreference.com states that "The random number generator is implementation-defined, but the function std::rand is often used." I've seeded that with std::srand, but apparently that's not completely portable.

查看更多
Viruses.
5楼-- · 2020-06-29 03:14
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>

using namespace std;

class Random {
public:
    Random(){
        srand( static_cast<unsigned int>(time(NULL)));
    }

    unsigned int operator()(unsigned int max){
        double tmp = static_cast<double>( rand() ) / static_cast<double>( RAND_MAX );
        return static_cast<unsigned int>( tmp * max );
    }
};

int main(int argc, char** argv) {
    int random_numbers[8];
    int size = sizeof(random_numbers)/sizeof(int); 
    for(int i=0;i<size;++i)
        random_numbers[i]=i;
    Random r;
    random_shuffle(random_numbers, random_numbers + size, r);
    for (int i = 0; i < 8; i++) {
        cout << random_numbers[i] << endl;
    }
    return 0;
}
查看更多
登录 后发表回答