-2

So basically i wrote this function in C to generate 5 random numbers from 1 to 50:

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

int main() {
    int c, n;

    printf("Five random numbers from 1 to 50 \n");

    for (c = 1; c <= 5; c++) {
        n = rand()%50 + 1;
        printf("%d\n", n);
    }

    return 0;
}

and i'd like to know how can i be sure that the numbers generated by this code are all different from each other.

Any help ?

laker02
  • 107
  • 1
  • 1
  • 6
  • you can't that's the nature of random, if you store the previous you can compare to those but otherwise you cannot. In-fact with `rand` which only has 16 bits of very poor pseudo randomness it's almost a guarantee. This particular setup won't produce a uniform distribution either. If you want to use a better random I would suggest [`random_r`](http://man7.org/linux/man-pages/man3/srandom_r.3.html) – Mgetz Jan 05 '14 at 23:44
  • 1
    If you want five *distinct* numbers from 50 you can shuffle a sequence of 1..50 (using `rand()` and a decent shuffle-algorithm such as a seven-pass-swap), then just take the first five numbers off the sequence. There are other ways, but this is much akin to shuffling a deck of cards from your description. – WhozCraig Jan 05 '14 at 23:49
  • can you show a little example @Mgetz? – laker02 Jan 05 '14 at 23:51
  • @WhozCraig can you give me an example ? – laker02 Jan 05 '14 at 23:52
  • Sure. [See it live](http://ideone.com/tCM3UF). You should check out BLUEPIXY's answer below, however, as it is considerably more efficient. – WhozCraig Jan 05 '14 at 23:57
  • you didn't seed the random generator, so it will always produce the same series – phuclv Jan 06 '14 at 01:57
  • if i want to be sure that the numbers aren't all odd or all even, how would i do that? @WhozCraig – laker02 Jan 06 '14 at 15:41

3 Answers3

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

void swap(int *a, int *b){
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main() {
    int i, c, n, size = 50;
    int data[size];

    srand(time(NULL));
    for(i=0;i<size;++i)
        data[i] = i+1;

    printf("Five random numbers from 1 to 50 \n");

    for (c = 1; c <= 5; c++) {
        n = rand()%size;
        printf("%d\n", data[n]);
        swap(&data[--size], &data[n]);
    }

    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • +1 This is an *excellent* answer, and the OP would be wise to step through and see how it works. – WhozCraig Jan 05 '14 at 23:58
  • 2
    It's a good program, but don't just dump code for people to copy & paste. Add some explanation so that they may learn. – Kninnug Jan 06 '14 at 00:12
  • See: [How not to shuffle — The Knuth Fisher-Yates Algorithm](http://www.i-programmer.info/programming/theory/2744-how-not-to-shuffle-the-kunth-fisher-yates-algorithm.html), [Shuffle — Shuffle a deck of cards — Knuth shufle](http://tekpool.wordpress.com/2006/10/06/shuffling-shuffle-a-deck-of-cards-knuth-shuffle/) and [Wikipedia — Fisher-Yates Shuffle](http://tekpool.wordpress.com/2006/10/06/shuffling-shuffle-a-deck-of-cards-knuth-shuffle/). Or do your own assessment of the web. I used a Google search for 'knuth shuffle proof'. – Jonathan Leffler Jan 06 '14 at 00:37
  • @Kninnug I agree, and probably should have been more specific. its a good implementation, the "why" needs some flesh, to be sure. – WhozCraig Jan 06 '14 at 17:57
  • I think that there is no place that is difficult enough to require explanation. But I keep writing. (1)I will prepare the required number of the card. (2)I will fill in the numbers in the range required for the card. (3)I pull out select a card. (4)I repeat (3) as needed. – BLUEPIXY Jan 06 '14 at 18:33
0

Store each number in an array and check each new random number against the existing entries.

Ben Jackson
  • 90,079
  • 9
  • 98
  • 150
0

You should check all numbers like this:

#include <time.h>

int randomNumbers[5];
int c, d, n;
bool itIsNew;

//current time as random seed
srand(time(0));

for (c = 1; c <= 5;)
{
    n = rand() % 50 + 1;
    itIsNew = true;

    for(d = 1; d <= c; d++)
    {
        if(n == randomNumbers[d - 1])
        {
            itIsNew = false;
            break;
        }       
    }
    if(itIsNew)
    {
        randomNumbers[c - 1] = n;
        c++;
    }
}
Mustafa Chelik
  • 2,113
  • 2
  • 17
  • 29
  • 1
    Please learn to run your loops from 0 to less than the limit: `for (d = 0; d < c; d++)` etc; that way, you don't have to futz with `d - 1` in the subscripts. – Jonathan Leffler Jan 06 '14 at 00:02