0

I want the program to print randomly one of these numbers:

{1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10}

Instead it prints randomly nonsense numbers

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

int main()
{
  srand( time(NULL) );
  int card;
  int deck[40] = {1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10};
  card = rand()% deck[40];
  printf("%d", card);
  return 0;
}

What can I do? thanks

Ian Abbott
  • 15,083
  • 19
  • 33
Ognum
  • 19
  • 7

1 Answers1

2

This simple way to do this is

card = deck[rand() % 40]

This way you are picking a 'random' index in the 40 numbers of your array. Even tho with this array you have the same probability on every number.You can change the balance by changing the array.

ACoelho
  • 341
  • 1
  • 11
  • [You don't actually have the same probability on every number unless (RAND_MAX % 40) != 0](https://stackoverflow.com/q/10984974/2166798) – pjs Nov 30 '20 at 17:20