Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,095 questions

40,777 answers

573 users

How to generate random number for powerball lottery in C

1 Answer

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <time.h> 
#include <stdbool.h>

int main(void)
{
    int r, a, b, line[5] = { 0 };
	bool dup;
     
    srand((unsigned)time(NULL));
     
    printf("                          POWER\n");
	for (int n = 0; n < 4; n++)
	{
		a = 1;
		b = 69;
		for (int i = 0; i < 5; i++)
		{
			// rand() % (21) = between 0 - 20 + 10 = between 10 - 20
			r = rand() % (b - a + 1) + a; // between 1 - 69
			
			// Preventing duplicates numbers on a line
			for (int j = 0; j < 5 ; j++)
			{
				 if (line[j] != r)
					 dup = false;
				 else
				 {
					 dup = true;
					 break;
				 }
			}
			if (dup == false)
			{
				line[i] = r;
				printf("%3d ", r);
			}
			else
				i--; // start again and generate new number, instead the duplicate 
			
		}
		printf(" QP - ");
		
		a = 1;
		b = 26;
		for (int i = 0; i < 1; i++)
		{
			 r = rand() % (b - a + 1) + a; // between 1 - 69
			 printf("%3d  QP", r);
		}
		printf("\n");
	} 
    return 0;
}
   
    
/*
      
run:
      
                          POWER
 25   4  48  64  29  QP -   6  QP
 51  65   1  60  19  QP -   7  QP
 38  55   6  18  31  QP -  25  QP
 16  28  19  47  37  QP -  24  QP
 
*/

 





answered Jan 30, 2016 by avibootz
edited Jan 30, 2016 by avibootz

Related questions

2 answers 76 views
76 views asked Feb 28, 2021 by avibootz
2 answers 163 views
1 answer 71 views
1 answer 75 views
1 answer 68 views
...