-5

I need my program to make a random sequence from 1-3 each time, but I don't understand how I'd use rand() to make the sequence of numbers 1 to 3 in a different order each program. It can't be the same number again, so I don't know what I'd do to prevent that. An example run would be

123 the first, 231 the second, 321 and so fourth

What would you use to make a sequence that doesn't repeat numbers

Thrylin
  • 1
  • 2
  • You can visit [here](https://www.geeksforgeeks.org/stdnext_permutation-prev_permutation-c/) – Yunus Temurlenk Jan 20 '20 at 05:24
  • 1
    A truly random sequence CAN have repetition. – Peter Jan 20 '20 at 05:34
  • So you want to randomly select numbers from a sequence without replacement? You can just shuffle it randomly once, and then pop from the back until exhausted. – Jan Christoph Terasa Jan 20 '20 at 05:39
  • Wait a second: Do you not want to have repetitions *only within* these 3-digit sequences or don't you want to have repetitions *globally*? Would a sequence 123, 321 be legal? Would you want to produce *arbitrary* random sequences from these three-digit sequences? Would 123, 123, 231, 321, 123 be legal? – Aconcagua Jan 20 '20 at 07:12

3 Answers3

1

The simplest way to generate your sequence would be to use std::shuffle to re-order a vector containing your desired values:

#include <vector>
#include <algorithm>
#include <random>
#include <iostream>

int main()
{
    std::random_device rd;
    std::mt19937 g(rd());
    std::vector<int> elements = { 1, 2, 3 };
    std::shuffle(elements.begin(), elements.end(), g);
    for (int i : elements)
    {
        std::cout << i << "\n";
    }
}

If you really must use rand() (its not generally a very good random number generator) you can just about squeeze it into shuffle too:

#include <vector>
#include <algorithm>
#include <ctime>
#include <iostream>

struct RandDevice
{
    using result_type = uint32_t;
    static result_type max() { return static_cast<result_type>(RAND_MAX); };
    static result_type min() { return 0; };

    result_type operator()() {
        return static_cast<result_type>(rand());
    }
};

int main()
{
    std::vector<int> elements = { 1, 2, 3 };
    srand(time(0));
    std::shuffle(elements.begin(), elements.end(), RandDevice());
    for (int i : elements)
    {
        std::cout << i << "\n";
    }
}
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
0

You can use std::next_permutation

Example here : https://en.cppreference.com/w/cpp/algorithm/next_permutation

Sitesh
  • 1,816
  • 1
  • 18
  • 25
  • It's a better tool to accomplish the goal, but doesn't use `rand`. – user4581301 Jan 20 '20 at 05:32
  • Maybe that's only half of the answer. We might produce all permutations with and then either just `std::shuffle` them or we might produce an array from all of these and pick arbitrary element from based on random number – depending on needs of user. There's yet some clarification necessary, though... – Aconcagua Jan 20 '20 at 07:11
0

it's pretty easy to do.. just compare the number you with every occupied element in the array. if it is not in the array, add to array. else, try again.

I have done similar code check this out

    #include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
void displayArray(int randNum[], int elements);
void randomNum(int randNums[], int elements);
int main ()
{
    //declare array
    int numbers[999] = {0};
    //random number generator
    srand(static_cast<int>(time(0)));

    randomNum(numbers, 999);
    displayArray(numbers, 999);

system("pause");
return 0;
}
void randomNum(int randNums[], int elements)
{
    for (int i = 0; i < elements; i++)
    {
        bool same;
        do
        {
            same = false;
            randNums[i] = rand() % 999 + 100;
            // Check if the newly generated number is a duplicate:
            for (int check = 0; check < i; check++)
            {
                if (randNums[i] == randNums[check])
                {
                    same = true;
                    break;
                }
            }
        } while (same);
    }
}
void displayArray(int randNum[], int elements)
{
    for (int sub = 0; sub < elements; sub ++)
    {
        cout << "Unique Numbers: " << randNum[sub] << endl;
    }
}
  • 2
    Modulo on random number generator's results produce rather bad distribution. And danger of collisions rises the more elements you have already filled in. There isn't even a guarantee that you ever get your array filled completely. Apart from bad distribution, `%999 + 100` can produce unwanted sequences like 497, 101, 1098, ... – Aconcagua Jan 20 '20 at 07:20
  • About [using namespace std](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – and `system("pause")` or similar stuff (`getc()`, checking with `kbhit` from `conio.h`) is bad practice as well. It's *not* the task of a console programme to keep the console window open! You only prevent your programme being usable from scripts. – Aconcagua Jan 20 '20 at 07:27