1. ## Random Generation

Hi guys!

I'm writing a program that involves with random generation. Basically, I want to randomly generate a range of values, lets say from 1 to 10 to an array of 10 element. Another thing is I also want those generated values to not overlap each other like (1,2,2,3,3,4,5...) something like that. I want to guarantee that each value will only happen once in the array from a certain range.
I hope that u guys can give me a hand and help me with this, really appreciate that ^_^

2. Cool.

What have you done so far?

3. Code:
```#include <iostream>
#include <cstdlib>
#include <ctime>
#define MAX 20

void randomvalues(int *l, int n) {
srand(time(0));
for(int i=0; i<n;i++) {
l[i] = (rand()%n);
}
}

int main() {
void randomvalues(int *l, int n);

int a[MAX];

randomvalues(a, MAX);```
This is what i got so far. I still couldn't find a way to make it none overlapping.

4. The solution that you want is going to somehow break down to using an array (though, if the numbers only go from 1 -> 10, you could use an int.)

You'll want to save off that a number had been used and generate a new number until you find one that isn't used. That, or count up/down from your used position until you find some number that isn't used, the choice is up to you.

5. Start with an array of appropriate size and populated with the numbers 1,2,3...n and then randomize it by swapping, numerous times, random element a with random element b. You will thus avoid duplicates appearing. This will also allow you to insert duplicates at some later point if necessary.

6. You just need an equation that gives different results every time. You should store a value that
keeps incrementing ( or else ), and you'll pass it to the eq to get a 'random' number.

Code:
```int equation()
{
return 2*x-1;
}

int x = 0; // Or any other value

int rValue = equation(x++);```
Of course, that's one way of doing this...

7. ## .

maybe something like this..

Code:
```    int num_array[10] = //initialise  1 to 10
int count = 0;
int results_array[10] = { 0 };

int num = 0;

while(count < 10)
{
num = //choose rand out of 10

if(num_array[num] != 0) //if not already used
{
results_array[count] = num_array[num]; //fill array
num_array[num] = 0; // set to used
count++;
}
}```
if thats the sort of thing you mean, i dunno, this is similar to what fossaw said

8. I second Fossaw's idea, though not the exact suggestion as the shuffling method described is not particularly good if you want a uniform distribution, and besides... use std::random_shuffle().

9. It has the advantage of being easy to understand and code for a relative newcomer.

10. thanks for the suggestion guys!
just a curious question, random_shuffle() does what? is it efficient to random a array?

11. Originally Posted by davewang
thanks for the suggestion guys!
just a curious question, random_shuffle() does what? is it efficient to random a array?
Look here and especially the example.
Generally, it radnomizes the elements of a structure, like a std::vector.
It is efficient in general and it only lets you use random access iterators (thus you wouldn't be able to use a std::list, but a std::vector). Exactly how efficient I don't know, you will need to look at the implementation of the function.

12. Originally Posted by davewang
random_shuffle() does what?
It shuffles the elements in the specified range with uniform distribution. This range can be the entire range of an array, but as C_ntua noted, it must have random access to the range, and certainly pointers to the elements of an array qualify.

Originally Posted by davewang
is it efficient to random a array?
It is as efficient as what Fossaw described, if you interpret "numerous times" to be "one time less than the number of elements in the array". But it is better if you want a uniform distribution, and of course better in the sense that you do not have to code it yourself so you reduce the chance of introducing a bug.