Home > database >  how can i make two equal random numbers in c
how can i make two equal random numbers in c

Time:12-28

I'm new to coding and i need help with my project: so, what i need to do is code a "finding the two same cards game", in which every card has a number and we're basically trying to find that two equal numbers. those numbers should be generated with rand() function but i cannot think a way to how to make two random numbers equal to each other if I'm using the rand() function. i don't know if i worded this the best way but i hope you get what i mean and if you don't I'm open to explain it with an example.

thanx in advance!

CodePudding user response:

Just create half as many elements and duplicate them. You can actually take consecutive numbers and shuffle them. This way you make sure every number is repeated exactly once:

int main()
{
   enum { size = 10 };

   int cards[size];

   for (int i = 0; i != size / 2;   i)
   {
      cards[i] = i;
      cards[size/2   i] = i;
   }

   // now shuffle the array
}
  •  Tags:  
  • c
  • Related