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 ifthisnew 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
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.
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);
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.
#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.
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.
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.
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.
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?)
#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;
}