The test of independence of two groups of variables is addressed in the case where the sample size N is considered randomly distributed. This assumption may lead to a more realist testing procedure since in many situations the sample size is not known in advance. Three sample schemes are considered where N may have a Poisson, Binomial or Hypergeometric distribution. For the case of two groups with p1 and p2 variables, it is shown that when either p1 or p2 (or both) are even the exact distribution corresponds to a finite or an infinite mixture of Exponentiated Generalized Integer Gamma distributions. In these cases a computational module is made available for the cumulative distribution function of the test statistic. When both p1 and p2 are odd, the exact distribution of the test statistic may be represented as a finite or an infinite mixture of products of independent Beta random variables whose density and cumulative distribution functions do not have a manageable closed form. Therefore, a computational approach for the evaluation of the cumulative distribution function is provided based on a numerical inversion formula originally developed for Laplace transforms. When the exact distribution is represented through infinite mixtures, an upper bound for the error of truncation of the cumulative distribution function is provided. Numerical studies are developed in order to analyze the precision of the results and the accuracy of the upper bounds proposed. A simulation study is provided in order to assess the power of the test when the sample size N is considered randomly distributed. The results are compared with the ones obtained for the fixed sample size case.