-2

I am trying to write a code that can generate 3 random integers from 1 to 100. I thought of using array to store the integers, but I have no idea how I am going to access those 3 random integers. I want my code to return something like: 5 92 66. Is there any better way to go about it?

#include <iostream>

using namespace std;

int main()
{
    int nums[100];
    for(int index = 1; index < 101; index++)
    {
        nums[index] = 1;
    }

    return 0;
}

I expect output to be something like: 88 17 3.

AbdelAziz AbdelLatef
  • 3,650
  • 6
  • 24
  • 52
Real G
  • 37
  • 2
  • 3
    What is your question? How to generate random numbers? How to store numbers? How to access "however you stored them" somewhere else? Your use of arrays (indexing 1-100) suggests you don't know C++ very well. You may be better off looking at a text book or an online course first. – John3136 Oct 08 '19 at 21:40
  • You can only use the 100 locations in `nums` from 0 to 99. – stark Oct 08 '19 at 21:42

1 Answers1

0
Create a set S.
Repeat while number of elements in S < 3
    Create random number N with 1 <= N <= 100
    If number N not in S
        Insert N into S

Now S contains 3 different random numbers. You can create random numbers with std::uniform_int_distribution

Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62