-5

I want to create a program which prompts the user for an amount of random numbers to generate (between 1 and 20). The program then generates the amount of numbers between (1 and 40). The numbers generated should all be unique.

I don't know how to get them to produce a unique number.

haccks
  • 104,019
  • 25
  • 176
  • 264
cybin
  • 9
  • 3
  • @woolstar; This guy is telling you in Chinese: `I don't know.` – haccks Dec 30 '13 at 02:30
  • @haccks u are right :P – cybin Dec 30 '13 at 02:40
  • @user3145072 what you mean by unique? – rullof Dec 30 '13 at 02:42
  • @rullof the amount of numbers between 1 and 40 – cybin Dec 30 '13 at 02:44
  • 2
    Given the limits you're imposing, you can create an array initialized to zeros for the numbers 1..40. Each time you generate a number, check whether it was already used, and if not record that it was generated by setting the element of the array to one. Make sure you don't get into an infinite loop by trying to generate 30 unique integers in the range 1..20. This assumes you know about [`rand()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/rand.html) and [`srand()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/srand.html). There are other better PRNG around, too. – Jonathan Leffler Dec 30 '13 at 02:45
  • possible duplicate of [Random array generation with no duplicates](http://stackoverflow.com/questions/20734774/random-array-generation-with-no-duplicates) – rullof Dec 30 '13 at 02:50
  • 1
    @rullof: closely related, but not quite identical. Shuffling an array initialized with values 1..40 and stopping when you've shuffled the first 20 values certainly works. For this scale of problem, the differences are negligible. There are other techniques too. Jon Bentley [Programming Pearls](http://www.amazon.com/Programming-Pearls-2nd-Jon-Bentley/dp/0201657880) does a good job discussing them, IIRC. – Jonathan Leffler Dec 30 '13 at 02:53
  • @rullof; Read my answer. – haccks Dec 30 '13 at 02:54

1 Answers1

3

If you want to generate unique random numbers each time you run the program then try this;

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>

int main(void)
{
    int n, num;
    printf("Enter the amount of numbers to generate");
    scanf("%d", &n);
    bool temp[40];
    srand((unsigned)(time(NULL)));

    for (int i=0; i<n+1; i++){
        temp[i] = false;
    }

    for (int i=0; i<n;)
    {
        num = 1 + rand()%40;
        if(temp[num] == false)
        {
            printf("%d\n", num);
            temp[num] = true;
            i++;
        }

    }
    return 0;
}  
haccks
  • 104,019
  • 25
  • 176
  • 264
  • @haccks this is good except a probability problem, read that paragraph of my answer here http://stackoverflow.com/a/20735198/2727656. – rullof Dec 30 '13 at 03:03
  • @rullof; Suffling is good. But as Jonathan Leffler pointed out in his comment that for small scale the portability issue can be neglected. – haccks Dec 30 '13 at 03:14