Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //random number generator from 1 to n
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #define CHECKED 1
- int *flag;
- int* rand_range(limit)
- {
- //random array generator
- int *random_nos, i, no, ctr = 0;
- random_nos = (int *) calloc(limit + 1, sizeof(int));
- flag = (int *) calloc(limit + 1, sizeof(int));
- srand(time(NULL));
- while (ctr < limit)
- {
- no = rand() % limit + 1;
- if (flag[no] != CHECKED)
- {
- flag[no] = CHECKED;
- random_nos[ctr] = no;
- ctr ++;
- }
- }
- return random_nos;
- }
- void print_list(int *ar, int size)
- {
- int i;
- printf("\n");
- for (i = 0; i < size; i ++)
- printf("%d\t", ar[i]);
- printf("\n");
- }
- int main()
- {
- int *ar, n;
- printf("\nEnter upper limit: ");
- scanf("%d", &n);
- ar = rand_range(n);
- print_list(ar, n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement