Not repeating random numbers

Hi. So this is my working code. Only problem is, random numbers are occasionally repeating themselves and obviously I need unique random numbers that don't repeat. I have tried to solve this on my own and have googled the answer I'm looking for but I still can't solve this problem.

Any help would be appreciated!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(){

	srand((int)time(NULL));

	int i;
	int randvalue[5];

	for(i=0; i < 5; i++){
	randvalue[i] = rand() % 99 + 1;

			 }
	printf("\nYour numbers are: \n%d %d %d %d %d\n", randvalue[0], randvalue[1], randvalue[2], randvalue[3], randvalue[4]);

	int j;
	int rvalue[5];

	for(j=0; j < 5; j++){
	rvalue[j] = rand() % 99 + 1;
			    }


	printf("\nThe winning numbers are: \n%d %d %d %d %d\n\n", rvalue[0], rvalue[1], rvalue[2], rvalue[3], rvalue[4]);

	int match = 0;
	int k, l;

	for (k = 0; k < 5; ++k){
	for (l = 0; l < 5;++l){
        if (randvalue[k] == rvalue[l])
        {
           match++;
        }
    			 }
			 }

	if (match == 1)
	printf("You matched %d number.\n", match);
	else if (match >= 3)
	printf("Congratulations! You are a winner!. You matched %d numbers!\n", match);
	else
	printf("You matched %d numbers.\n", match);

return 0;

}
Found something that worked. Any advice would be appreciated.

[edit] Just drew a duplicate number. Although this picks duplicate numbers much less frequently, it seems it does still occasionally pick duplicates. Back to the drawing board. Again, any help would be appreciated.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define randrange(N) rand() / (RAND_MAX/(N) + 1)
#define MAX 99

static int randvalue[5];
int candidates[MAX];

int main (void) {
  int i;

  srand(time(NULL));

  for (i = 0; i < MAX; i++)
    candidates[i] = i;

  for (i = 0; i < MAX-1; i++) {
    int a = randrange(MAX-i);
    int b = candidates[i];
    candidates[i] = candidates[i+a];
    candidates[i+a] = b;
  }

  for (i = 0; i < 5; i++)
    randvalue[i] = candidates[i] + 1;

    printf("Your numbers are: %i %i %i %i %i\n", randvalue[0], randvalue[1], randvalue[2], randvalue[3], randvalue[4]);


static int rvalue[5];

int j;

for (j = 0; j < MAX; j++)
    candidates[j] = j;

  for (j = 0; j < MAX-1; j++) {
    int c = randrange(MAX-i);
    int d = candidates[i];
    candidates[j] = candidates[j+c];
    candidates[j+c] = d;
  }

  for (j = 0; j < 5; j++)
    rvalue[j] = candidates[j] + 1;

    printf("The winning numbers are: %i %i %i %i %i\n", rvalue[0], rvalue[1], rvalue[2], rvalue[3], rvalue[4]);


int match = 0;
	int k, l;

	for (k = 0; k < 5; ++k){
	for (l = 0; l < 5;++l){
        if (randvalue[k] == rvalue[l])
        {
           match++;
        }
    			 }
			 }

	if (match == 1)
	printf("You matched %d number.\n", match);
	else if (match >= 3)
	printf("Congratulations! You are a winner!. You matched %d numbers!\n", match);
	else
	printf("You matched %d numbers.\n", match);

  return 0;
}
Last edited on
You could just fill an array with unique numbers, and then shuffle the array.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <algorithm>
#include <random>

std::mt19937 random{ std::random_device{}() };

int main() {

	const int num_numbers = 10;
	int numbers[num_numbers] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

	std::shuffle(std::begin(numbers), std::end(numbers), random);

	for (int i : numbers) {

		std::cout << i;

	}

	return 0;
}
Last edited on
xismn, thanks for the reply. I will keep that in mind when programming in C++.

For anyone interested in the subject. I solved my problem. Program is now producing unique numbers with no duplicates.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <getopt.h>

#define randrange(N) rand() / (RAND_MAX/(N) + 1)
#define MAX 75

void getNumbers();

static int randvalue[5];
int candidates[MAX];
int x;

int main (int argc, char **argv) {

	srand(time(NULL));
	int pmultiplier = rand() % 15 + 1;
	int gmultiplier = rand() % 15 + 1;

	while ((x = getopt (argc, argv, "pgoh")) != -1)
		switch (x)
      		{
	case 'p':
	
	getNumbers();

	printf("Your numbers are: %i %i %i %i %i [%i]\n", randvalue[0], randvalue[1], randvalue[2], randvalue[3], randvalue[4], pmultiplier);

        break;

	case 'g':

	getNumbers();

	printf("The winning numbers are: %i %i %i %i %i [%i]\n", randvalue[0], randvalue[1], randvalue[2], randvalue[3], randvalue[4], gmultiplier);
	
	break;

	case 'o':
	printf("5 numbers plus multiplier - 1 in 259 million (Jackpot!)\n");
	printf("5 numbers - 1 in 18.5 million ($1,000,00.00)\n");
	printf("4 numbers plus mulitplier - 1 in 740 thousand ($5,000.00)\n");
	printf("4 numbers - 1 in 52.8 thousand ($500.00)\n");
	printf("3 numbers plus multiplier 1 in 10.7 thousand ($50.00)\n");
	printf("3 numbers - 1 in 766 ($5.00)\n");
	printf("2 numbers plus mulitplier - 1 in 473 ($5.00)\n");
	printf("1 number plus mulitplier - 1 in 56.5 ($2.00)\n");
	printf("1 mutiplier - 1 in 21.4 ($1.00)\n");
	break;

	case 'h':

	printf("Usage\n");
	printf("-p pick lottery numbers\n");
	printf("-g generate winning numbers\n");
	printf("-o print odds and payouts\n");
	printf("-h print this screen\n");

	}

  return 0;
}

void getNumbers(){

	int i;
	
	for (i = 0; i < MAX; i++)
	candidates[i] = i;

	for (i = 0; i < MAX-1; i++) {
	int a = randrange(MAX-i);
	int b = candidates[i];
	candidates[i] = candidates[i+a];
	candidates[i+a] = b;
				    }

	for (i = 0; i < 5; i++)
	randvalue[i] = candidates[i] + 1;
		}
Last edited on
Topic archived. No new replies allowed.