0

I'm trying to produce random numbers between 1-10 without any repetition but it's not working. Is there any way to do this without using arrays?

#include<iomanip> 
#include<cstdlib> 
#include<ctime>
#include<time.h>

using namespace std;

int main()
{
    
    srand(time(0));
    int randomNumber=0; 
    
    
   for (int i=0; i<10;i++)
      { 
      randomNumber=(rand() % 10) + 1;
      
      cout<<randomNumber<<endl;
     } 
}
NatanataC
  • 3
  • 1
  • 3
  • 1
    Not really. One way or another, you'd need to keep track of numbers already used up. If not in an array, then in some other data structure. – Igor Tandetnik May 29 '22 at 01:26
  • Why, why, why, would you come up with a "no array" restriction? If it's not your restriction, but the teacher, state it, and also push back on the stupid requirement – Jeffrey May 29 '22 at 01:26
  • 1
    Is using the individual bits of an int considered an array ? If not, do that. – Jeffrey May 29 '22 at 01:27
  • Yeah because my teacher haven't taught arrays yet and she wanted us to only use what she had taught :( – NatanataC May 29 '22 at 01:30
  • 2
    @NatanataC -- We have no idea what you have or haven't been taught. `std::set s; while (s.size() < 10) s.insert(rand()%10 + 1);` -- That doesn't use arrays. – PaulMcKenzie May 29 '22 at 01:50
  • [This documentation page for `std::iota`](https://en.cppreference.com/w/cpp/algorithm/iota) provides a sane way to accomplish the task. Sad that you appear to require an insane way. – user4581301 May 29 '22 at 02:27
  • 1
    Simply create an array or vector holding the numbers 1-10 and *Shuffle* the array (or vector). – David C. Rankin May 29 '22 at 03:47

2 Answers2

2

You can ensure every random number is unique by permutating an array of 1 to 10 like this:

#include<iomanip> 
#include<cstdlib> 
#include<ctime>
#include<time.h>

#include <ranges>
#include <algorithm>
#include <iostream>

int main()
{
    srand(time(0));

    auto t = std::ranges::iota_view{1, 10};
    std::array<int, 10> nums;
    std::copy(t.begin(), t.end(), nums.begin());
    
    for (int i=0; i < 10 - 1; i++)
    { 
        int randomNumber = rand() % (10 - i);
        if (randomNumber > 0)
            std::swap(nums[i], nums[i + randomNumber]);
        std::cout << nums[i] << std::endl;
    }
}
Goswin von Brederlow
  • 11,875
  • 2
  • 24
  • 42
  • 2
    I like the simplicity of your solution of permutating an array, but Why would you not just use std::shuffle as well? – Taekahn May 29 '22 at 02:15
  • Because using a cryptographically sound UniformRandomBitGenerator would have been to professional. :) – Goswin von Brederlow May 29 '22 at 02:24
  • Knuth shuffle algorithm, so cool – ramsay May 29 '22 at 02:49
  • 1
    Why use C `rand()` and not include C++ `random` using `std::random_device`? You can use `std::shuffle` at that point. – David C. Rankin May 29 '22 at 03:51
  • no reason, I just kept the original code – Goswin von Brederlow May 29 '22 at 04:07
  • 1
    Gotcha. Generally it is helpful to demonstrate the current proper approaches to the OP to ensure they know that C++ provides a manner to accomplish the same thing without relying on legacy C implementations. (they may have grabbed that code from some outdated site or book). It's fine to reuse their code, but would be worth noting the C++ [Pseudo-random number generation](https://en.cppreference.com/w/cpp/numeric/random) scheme. – David C. Rankin May 29 '22 at 04:27
-3

You can use an unordered map to ensure that the random number generated is unique:

#include<bits/stdc++.h>
using namespace std;

int main()
{

srand(time(0));
int randomNumber=0; 
unordered_map<int,bool> m;
while(m.size()<10)
{
randomNumber=rand()%10+1;
if(m.count(randomNumber)>0)
  continue;
m[randomNumber]=true;
cout<<randomNumber<<endl;
}
return 0;
}