Reputation:
My intention is to generate random numbers from 1 to 9 without repetition
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int randrange(int low,int high) /* generates a random number within given range*/
{
return rand()%(low+high)+low+1;
}
int main()
{
int num[9]={0},i,j;
bool check;
for(i=0;i<9;i++)
{
check=false;
do
{
num[i]=randrange(1,9);
for(j=0;j<i;j++)
{
if( num[i]==num[j]) // checks whether number already exists in the array
check=false;
else
check=true;
}
} while(check==false);
}
// the program is working fine without the repetition check
// this section prints out the array elements
for(i=0;i<9;i++)
{
cout<<num[i]<<" ";
}
return 0;
}
Upvotes: 5
Views: 35354
Reputation: 1
This code works fine for me to generate an array of random numbers from 1 to 9 without repetition
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int main(){
srand(time(0));
int arr[10];
for(int i=0; i<9; i++){
arr[i] = i+1;
cout << arr[i] << " ";
}
int j=0;
int temp=0;
cout << "\n\n\n-------------------------\n\n\n";
for(int i=0; i<9; i++){
j = (rand()%8)+1;
temp = arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
for(int i=0; i<9; i++){
cout << arr[i] << " ";
}
}
Upvotes: 0
Reputation: 1
#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
using namespace std;
void rnd(vector<int> &v, const int n){
for (size_t i=0;i<=n;++i){
v.push_back(i);
}
random_shuffle(v.begin(), v.end());
}
void PrintVector(const vector<int> &x){
for (size_t i=0;i<x.size(); ++i){
cout<<x[i]<<'\t';
}
}
int main(){
vector<int> a;
rnd(a,10);
PrintVector(a);
}
Upvotes: 0
Reputation: 47784
Okay, you already might have figured out the problem by dasbinkenlight's answer
And in addition to Peter's answer, you can use std::map
too to achieve unique random numbers:
std::map<int,int> m;
srand (time (NULL));
for(i=0;i<9;i++){
do{
j=randrange(1,9);
}while(m.find(j)!=m.end());
m[j]; //insert into map, no need for value.
num[i]=j;
}
Upvotes: 0
Reputation: 29
Your program is probably looping. It's bit hard to read your code because of your weird indentation, but it looks like there's a logic flaw in your for loop here:
check=false;
do
{
num[i]=randrange(1,9);
for(j=0;j<i;j++)
{
if( num[i]==num[j]) // checks whether number already exists in the array
check=false;
else
check=true;
}
} while(check==false);
You might want to remove the second check=false;
line to do what I think you're trying to do.
Upvotes: 0
Reputation: 46559
Your program has a number of flaws, one of which is the range of random numbers that the randrange
function returns. It's not 1 to 9!
The direct cause of your program (the hanging of the program) is however, that you set check
to false
, then perform a loop that does nothing (because the first time around, i
is 0
and the inner loop with j
is never performed), so check
will always be false
.
Check the other answers for solutions.
Upvotes: 0
Reputation: 726509
Your duplicate checking loop has a flaw: the check
is set to the result of checking the last pair of values, rather than the result of checking all the preceding pairs.
You need to set check = true
before the inner loop, then proceed to verifying all items from zero to i-1
. If the check becomes false
at any point, stop the loop:
check = true;
for (j = 0 ; (check) && (j < i) ; j++) {
check = (num[i] != num[j]);
}
In addition, you need to fix the randrange
, because your current implementation returns values in range 2..11
:
int randrange(int low,int high)
{
return rand()%(high-low+1)+low;
}
Upvotes: 5
Reputation: 54270
Just generate the numbers 1 to 9, then shuffle them randomly using std::random_shuffle
.
int nums[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
std::random_shuffle(nums, nums + 9);
This will leave nums
with the numbers from 1 to 9 in random order, with no repetitions.
Upvotes: 11