0

- I've Implemented a very simple non repeated random generator and I want to share it for anyone ..
- I want Your Opinions Please , and any comments about my code
- 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
7 Years
Discussion Span
Last Post by VernonDozier
1

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.

0

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

0

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
}
0

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...

0

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

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.