- I've Implemented a very simple non repeated random generator and I want to share it for anyone ..
- Anyone notice any fault in the result (any repeated numbers), please report me
- Thank You Very Much
- ENJOY :D

``````#include <iostream>
#include <ctime>
using namespace std;
bool isContained(int arr[],int n,int x)
{
for(int i=0;i<n;i++)
{
if(arr[i]==x)
return true;
}
return false;
}
int main()
{
srand(time(0));
const int x = 100;
int r[x];
for(int i=0;i<x;i++)
{
r[i]=1+rand()%x;
while(isContained(r,i,r[i]))
{
r[i]=1+rand()%x;
}
}
for(int i=0;i<x;i++)
cout<<r[i]<<"\n";
cout<<"\n\n";
return 0;
}``````

- I don't Know if this code was implmented before

5
Contributors
6
Replies
7
Views
8 Years
Discussion Span
Last Post by VernonDozier

The code has two obvious problems. First, its time complexity is at best quadratic. Second, the randomness is not guaranteed.
Given that there are linear algorithms with a true random results (see, for example, shuffling), I don't think that this code has any value.

thank you for your advice , I always forget to check for complexity ,
I'm moderate in c++ , or beginner I think ..

what you are looking for are more along the lines of this :

``````void populateUniqueRandom(int Array[], const int size, int start, int end){
assert(end-start == size); //make sure the size the correct size
for(int i = 0; i < size; ++i){
Array[i] = end - start + i;
}
std::random_shuffle(Array,Array+size);
}
int main(){
const int Size = 5;
int Array[Size] = {};
populateUniqueRandom(Array,10,10+Size); //random number from 10 to 10+Size
}``````

what you are looking for are more along the lines of this :

``````void populateUniqueRandom(int Array[], const int size, int start, int end){
assert(end-start == size); //make sure the size the correct size
for(int i = 0; i < size; ++i){
Array[i] = end - start + i;
}
std::random_shuffle(Array,Array+size);
}
int main(){
const int Size = 5;
int Array[Size] = {};
populateUniqueRandom(Array,10,10+Size); //random number from 10 to 10+Size
}``````

Do you mind explaining your code? Specifically, the call on Line 11? There are only 3 arguments there, but your function has 4 parameters... I think there's something you missed...

Oh, yes thanks for the catch. It should be rather

``populateUniqueRandom(Array,Size,10,10+Size);``

Come to think of it I think there is a problem with the above algorithm I posted. It should be rather this :

``````void populateUniqueRandom(int Array[], const int size, int start, int end){
assert(end-start == size); //make sure the size the correct size
for(int i = 0; i < size; ++i){
Array[i] = start + i;
}
std::random_shuffle(Array,Array+size);
}``````

Edited by firstPerson: n/a

I have a few code snippets that might be of interest. I've been meaning to post some more or at least post some diagrams that explain the algorithm a little bit, but I haven't gotten around to it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.