-1

I am trying to generate 7 numbers in C. The code should contain:

  • It mustn't start with 0.
  • The numbers must be between 0-9
  • Numbers must be different from each other. (For example: there can't be two 5s, like this one: 7 5 8 3 2 5 4).

My code is working. It doesn't start with 0. Numbers are randomly generated between 0-9.

But I can't manage to include third thing. Same numbers are coming when I start the code. Do you know how can I generate all differently?

#include <stdio.h>
#include <stdlib.h>
#include <conio.h> 
#include <locale.h>
#include <time.h>

int main() {
  srand(time(NULL));
  int numbers[10];
  int i;
  for (i = 0; i < 7; i++) {
    numbers[i] = rand() % 10;
    if (numbers[0] == 0) {
      numbers[0] = 1 + rand() % 9;
    }
    printf(" %d ", numbers[i]);
  }  
  getch();
  return 0;
}
ocrdu
  • 2,172
  • 6
  • 15
  • 22
Bolin
  • 1
  • 1
  • 1
    If all numbers should be distinct, (1) fill the array with the sequence 1-thru-10. (2) [shuffle](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle) the sequence. The result will be the random unique sequence you seem to desire. – WhozCraig Jan 06 '21 at 10:57
  • I agree with the suggested solution by @WhozCraig, and then if the number starts with a 0, swap the 0 with a random one of the other 6 numbers. – Cheatah Jan 06 '21 at 11:03
  • I must do this without using the shuffle. I forgot to mention. – Bolin Jan 06 '21 at 11:04
  • 3
    Well, that's lovely and also quite moronic on the instructors side. The result is an random-based O(N^2) vs concrete-O(N) algorithm, completely unnecessarily. Hats off to the genius that teaches that malarky. – WhozCraig Jan 06 '21 at 11:24
  • @WhozCraig I have nothing to add. – Cheatah Jan 07 '21 at 15:17

2 Answers2

2

Using permutation (Knuth shuffle algorithm)

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

static int rrand(int range)
{
    return (int)((double)range * (rand() / (RAND_MAX + 1.0)));
}

static void randomize(int arr[], int size)
{
    while (size > 1)
    {
        int item = rrand(size--);
        int temp = arr[size];

        arr[size] = arr[item];
        arr[item] = temp;
    }
}

int main(void)
{
    int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    int size = sizeof arr / sizeof *arr;

    srand((unsigned)time(NULL));
    while (arr[0] == 0)
    {
        randomize(arr, size);
    }
    for (int i = 0; i < 7; i++)
    {
        printf("%d\n", arr[i]);
    }
}
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • 1
    this seem to be a much more efficient solution than the one i posted :) anyway apart for the first digit printed, it should include 0 in the range (which can easily be fixed) – Tsak Jan 06 '21 at 11:16
  • 1
    Exercise for the OP: Start the sequence with zero rather than one, and swap it to some random pick excluding slot-0 if it ends up back in slot-0 after the shuffle. (apparently zero is allowed, just not in the first slot). Props for using fp-div to eliminate modulo-bias. – WhozCraig Jan 06 '21 at 11:18
  • Oh thanks, I didn't pay attention to this requirement :( fixed – David Ranieri Jan 06 '21 at 11:21
1

im a beginner so my solution may not be the best but you could use memchr function to search into what you already filled in your tab it could be something like :

#include <stdio.h>
#include <stdlib.h>
#include <conio.h> 
#include <locale.h>
#include <time.h>
#include <string.h>
int main()
{

    srand(time(NULL));
    int numbers[10];
    int i;
    for (i = 0; i < 7; i++) {
        numbers[i] = rand() % 10;

        if(numbers[0] == 0){
            numbers[0] = 1 + rand()%9;
        }

        else if (memchr(tab, tab[i], i)){
            numbers[i] = rand() % 10;
        }

        printf(" %d ", numbers[i]);
   }

   getch();
   return 0;

}

Edit : anyway by reading the comments posted under your question meanwhile, you may prefer the other solution for its way more optimised, cause mine is quite crappy the more you go into the tab the less chance you get to generate a valid number the more time it takes

Tsak
  • 133
  • 7
  • Not so bad for a small set like this one, but you should take care (as commented by @WhozCraig) of the modulo-bias, take a look: https://stackoverflow.com/questions/10984974/why-do-people-say-there-is-modulo-bias-when-using-a-random-number-generator – David Ranieri Jan 06 '21 at 11:49