Create a lottery program

Write a program that will produce 5 unique random numbers between 1 and 50, inclusive.

You must prevent duplicate numbers in each run, so figure out how to do that. The program
must remember what numbers it is already generated, and make sure it does not produce
any duplicates.
My teacher never give lecture about rand() and srand(time(NULL))
1
2
3
4
5
6
7
8
  srand(time(NULL));
(loop)
//generate a random number from 1 to 50
num = rand() % 50 +1;
find out if this new number is unique
if it is, store it in a variable or array, and increment count
if it is not, force the next iteration of the loop
(loop end: if count == 5, quit the loop, else run it again)


This is what i got so far, can anyone help me finish it??
Sooo I think for each iteration except the first one you'll need to check if its a new unique number rigght ? If its not then repeat it again without jumping to next iteration, think while loop is a good choise here
1
2
3
4
5
6
7
8
9
10
11
12
13
bool unique
while{
  unique=false
  num=rand%50+1
  if(i=0) unique = true
  if(i!=0){
     //checking here
  }
  if(unique){
    array[i]=num
    i+1
  }
}
closed account (1CfG1hU5)

in older dos language, i use randomize(); function in stdlib.h and time.h.

srand may work if you don't have randomize();. srand in the form below will
compile on cpp.sh web site.

 
srand((unsigned) time(NULL));


1
2
3
4
int i, numbers[5];

for (i = 0; i < 5; i++)  // 0 through 4 array elements
  numbers[i] = rand()  % 50 + 1;


if repeat numbers, then new random:

1
2
3
4
5
6
7
8
9
void repeat_check(numbers[], size)
 {
   int t, l;  // 2nd int is a lower case L

   for (t = 0; t < size; t++)
     for (l = t + 1; l < size; l++)  // so 0 element doesn't check self and cause new #
        if (numbers[t] == numbers[l])
             numbers[t] = rand() % 50+1;
  }


may be wise to call the repeat check function a few times. maybe twice will
guarantee results with a loop.

call checker from main() like this:

 
repeat_check(numbers, 5);


or

1
2
3
4
int i;

for (i = 0; i < 2; i++)
   repeat_check(numbers, 5);


2nd for loop of repeat check and if statement should show as indented.
Last edited on
This actually could be easier if you think it like shuffling pokers. All you need to do is to make an array of [1..50] and random shuffle it, and the first five element would be your answer.

1
2
3
4
5
int array[] = {1, 2, ... 50};
std::random_shuffle(begin(array), end(array));

int answer[5];
std::copy(array, array + 5, answer);
closed account (1CfG1hU5)

int array[] = { 1,...,50 }; could work well too.

run through array and land at a random element. like a spin-wheel
until it stops. except faster to choose a number.

is that random_shuffle a part of your language, or is that the concept only?
would be a nice function if built in.

closed account (1CfG1hU5)

here is a bubble type sort.:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void sort(int array[], int size)
 {
   int temp, t, l;  // 3rd int is lower case L

   for (t = 0; t < size; t++) 
    for (l = 0; l < size; l++)
      if (array[t] < array[l])  // switch to '>' and descend sorts
        {
          // swap 2 array elements 
          temp = array[t];
          array[t] = array[l];
          array[l] = temp;
        }
  }


just fixed the sort names from numbers to array. 10/08/14, 9:21pm.

Last edited on
random_shuffle is a c++ method in header <algorithm>
closed account (1CfG1hU5)
thanks for info about random_shuffle
closed account (1CfG1hU5)
here is a fantasy 5 lottery program I wrote.

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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// prototypes

void repeat_check(int array[], int size);
void bubble_sort(int array[], int size);

int main(void)
 {

   int values[5], i;

   srand(time(NULL)); 

   for (i = 0; i <= 4; i++)
      values[i] = rand() % 39+1;
   
   for (i = 0; i <= 4; i++)
    repeat_check(values, 5);

   bubble_sort(values, 5);

   printf("Fantasy 5 numbers: \n\n");

   for (i = 0; i <= 4; i++)
     printf("%d   ", values[i]);

   return 0;

 }

void repeat_check(int array[], int size)
 {
   int t, l;

   for (t = 0; t < size; t++)
    for (l = t + 1; l < size; l++)
      if (array[t] == array[l])
         array[t] = rand() % 39+1;
  }


void bubble_sort(int array[], int size)
 {
   int temp, i, j;

   for (i = 0; i < size; i++)
    for (j = 0; j < size; j++)
      if (array[i] < array[j])
        {
          temp = array[i];
          array[i] = array[j];
          array[j] = temp;
        }
  }


runs good on cpp.sh.

if you were to compile this on your own system, some delays would be good when doing repeat checks for multiple draws/rerunning the program. the same numbers can repeat if not given a couple of seconds when doing new ones.

repeat_check function has l = t +1 so that the first element of the array passed does not check self and remake a number needlessly.

line 51 of the program code, can change the '<' sign to '>' and will do a descending sort.

randomizing code can also be done this way.


 
srand ((unsigned) time(NULL));  // definition for the "randomize();" function in stdlib.h 


or

1
2
time_t t;
srand((unsigned) time(&t));  // this is an example in compiler looking at function info 


both of these extra examples work if you replace line 15 with one of them.

tested 12 seconds ago.
Last edited on
Your repeat_check has defect. In that function, you can't make sure it doesn't generate the same value.

i.e.
value[] = { 1, 1, 3, 4, 5 };

In your repeat_check, you'll notice that values[1] == values[0], then you re-generated a value. However you can't guarantee that it won't generate '1' again.
If you write the code in your teacher's way, your code should be:

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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
  int value[5];
  int count;

  srand(time(NULL));

  for (count = 0; count < 5; count++) {
    int k;
    value[count] = rand() % 50 + 1;
    for (k = 0; k < count; k++) {
      if (value[k] == value[count]) {
	count--;
      }
    }
  }

  for (count = 0; count < 5; count++) {
    printf("%d  ", value[count]);
  }
  printf("\n");

  return 0;
}
closed account (1CfG1hU5)
quoted:

Your repeat_check has defect. In that function, you can't make sure it doesn't generate the same value.

i.e.
value[] = { 1, 1, 3, 4, 5 };

In your repeat_check, you'll notice that values[1] == values[0], then you re-generated a value. However you can't guarantee that it won't generate '1' again.

 
if (array[t] == array[l])...


values is passed to function, but is called array[] inside function.

essentially

1
2
3
4
5
if (array[0] == array[1]) // is truth.  if both t and l are 0, numbers can easily be doubled

or

if (vales[0] == values[1]) // related to received parameter passed 


yes, is possible to get a repeat even with the offset. that is why is looped at least a few times. after 3 to 5 runs through, a double/repeat is very unlikely. no doubt could be improved.

Last edited on
closed account (1CfG1hU5)
liuyang, line 13 of your rewrite, why declare 'int k;' there ? is declared 5 times
or more by loop. put under 'int count;' above. usable anywhere inside main().
line 9 is better. program works less.
Last edited on
the idea here is to compare the generated number immediately with previously stored numbers. the K here is a counter to compare from 0 to last stored number.
the K is not necessary for code outside the 'for' statement, so it's OK to declare inside for loop. (assume no performance impact. no?)
Last edited on
closed account (1CfG1hU5)
very tired. the count integer would back up the k right because of
< and count-- yes?

doesn't matter if declared under for or beginning of main. functional.
i would declare early. count-- seems to be managing any backsteps.

closed account (1CfG1hU5)
the count--, simple and effective
This one is my favorite.

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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define PICK 5
#define BASE 50

int main(void)
{
	int nums[BASE];
	int i;

	srand(time(NULL));

	for (i = 0; i < BASE; i++)
		nums[i] = i + 1;

	for (i = 0; i < BASE; i++)
	{
		int r, t;
		
		r = rand() % BASE;
		t = nums[i];
		nums[i] = nums[r];
		nums[r] = t;
	}

	for (i = 0; i < PICK; i++)
		printf("%d  ", nums[i]);
	printf("\n");

	return 0;
}
closed account (1CfG1hU5)
45 of 50 array elements not printed to screen.

for (i = 0; i < BASE; i++)
nums[i] = i + 1;

nums[i] could only be 0 to 49 yes?

desired to start was 1 to 50


closed account (1CfG1hU5)
second for loop, nums[i] is 0 to 50

dump first loop?

fix r = rand() % BASE + 1;
The idea is to make an array of all possible choices

[1, 2, 3, 4, ...., 50]

And then we randomise the order of the elements

[4, 5, 7, 1, 8...]

Finally we pick first 5, which is our answer.

[4, 5, 7, 1, 8]
Topic archived. No new replies allowed.