I know that there are many questions about random numbers, but I am trying to make a code that generate n
numbers in the interval [0, k]. This numbers can repeat, but I don't want a lot of repetition of the same number, example:
For 5 numbers in [0, 10], I don't want: 1 1 3 3 3
.
For 10 numbers in [0, 10], I don't want: 1 3 3 3 3 4 4 4 8 8
.
For 5 numbers in [0, 10], I want something like : 0 2 3 3 4
.
For 5 numbers in [0, 10], I want something like : 0 0 1 2 4 5 5 5 7 10
.
Ok, this is not hard to do, I did it like this:
static void random_numbers(unsigned int n)
{
unsigned int i, interval;
interval = 1000000;
srand((unsigned int) time(NULL));
for(i = 0; i < n; i++)
{
printf("%u\n", (unsigned int) rand() % (interval + 1));
}
}
The problem is, I want to generate this numbers in ascending and descending order too. I was trying to make it with something like:
static void ascending(unsigned int n)
{
unsigned int i, acc;
srand((unsigned int) time(NULL));
for(i = 0, acc = 0; i < n; i++)
{
acc += (unsigned int) rand() % 10; /* For descending, just change + to - */
printf("%u\n", acc);
}
}
This code will generate random numbers, and sometimes will repeat numbers, that's what I want. But can generate, if n
is 100 for example, the maximum number 900. I have not figured out a way to put an interval on it.
I thought a way but is not good, using the random_numbers
function to generate the numbers, store them in array and sort them (ascending or descending) and after print it, but it will take time and memory.
The purpose of this code is generate data for testing sort algorithms. I don't want to download DataSets, I want to make my own function. The numbers will be printed on stdout
, and using the redirection on linux, I will store them in a file.
Thanks.
Aucun commentaire:
Enregistrer un commentaire