Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include <time.h>
  4.  
  5. int validate(int low, int high);
  6. int genRand(int limit);
  7. int validatePick(int pick, int first, int second, int third, int fourth, int fifth, int sixth, int seventh);
  8. void printNums(int n, int first, int second, int third, int fourth, int fifth, int sixth, int seventh);
  9. double calcOdds(int n, int k);
  10.  
  11.  
  12. int main () {
  13.  
  14. int largest, low, high, rv, group, limit, n, k, pick, ret;
  15. int first, second, third, fourth, fifth, sixth, seventh;
  16. double derp;
  17. srand(time(NULL));
  18.  
  19. printf ("LOTTERY GENERATOR\n");
  20. printf ("Enter the largest number in your LOTTERY:\n");
  21. printf ("Enter a value (between 39 and 59) : ");
  22.  
  23. largest = validate(low, high);
  24.  
  25. printf ("Enter the amount of numbers you MUST select in the LOTTERY:\n");
  26. printf ("Enter a value (between 3 and 7): ");
  27.  
  28. group = validate2(low, high);
  29.  
  30. printf ("Your random LOTTERY numbers are: ");
  31.  
  32. limit = largest;
  33.  
  34. first = second = third = 0;
  35. fourth = fifth = sixth = seventh = -1;
  36. first = genRand(limit);
  37. pick = genRand(limit);
  38. while(validatePick(pick, first, second, third, fourth, fifth, sixth, seventh) == 0){
  39. pick = genRand(limit);
  40. }
  41. second = pick;
  42. while(validatePick(pick, first, second, third, fourth, fifth, sixth, seventh) == 0){
  43. pick = genRand(limit);
  44. }
  45. third = pick;
  46. if(group > 3) {
  47. while(validatePick(pick, first, second, third, fourth, fifth, sixth, seventh) == 0){
  48. pick = genRand(limit);
  49. }}
  50. fourth = pick;
  51. if(group > 4) {
  52. while(validatePick(pick, first, second, third, fourth, fifth, sixth, seventh) == 0){
  53. pick = genRand(limit);
  54. }}
  55. fifth = pick;
  56. if(group > 5) {
  57. while(validatePick(pick, first, second, third, fourth, fifth, sixth, seventh) == 0){
  58. pick = genRand(limit);
  59. }}
  60. sixth = pick;
  61. if(group > 6) {
  62. while(validatePick(pick, first, second, third, fourth, fifth, sixth, seventh) == 0){
  63. pick = genRand(limit);
  64. }
  65. seventh = pick;
  66. }
  67.  
  68. printf ("%d %d %d %d %d %d %d\n", first, second, third, fourth, fifth, sixth, seventh);
  69.  
  70. n = group;
  71.  
  72. printNums(n, first, second, third, fourth, fifth, sixth, seventh);
  73.  
  74. k = group;
  75. n = largest;
  76.  
  77. ret = validatePick(pick, first, second, third, fourth, fifth, sixth, seventh);
  78.  
  79. printf ("\n%d\n", ret);
  80.  
  81. derp = calcOdds(n, k);
  82.  
  83. //printf ("%f", derp);
  84.  
  85.  
  86. }
  87.  
  88.  
  89. int validate(int low, int high) {
  90. low = 39;
  91. high = 59;
  92. int largest=0, counter=0;
  93.  
  94. scanf ("%d", &largest);
  95. while (counter == 0) {
  96. if (largest >= low && largest <= high) {
  97. printf ("Thank You!\n");
  98. counter++;
  99. }
  100. else {
  101. printf ("***Error! You entered '%d' ... Value must be between 39 and 59 inclusive!***\n", largest);
  102. printf ("Enter a value (between 39 and 59) : ");
  103. scanf ("%d", &largest);
  104. }
  105. }
  106.  
  107. return largest;
  108. }
  109.  
  110. int validate2 (int low, int high) {
  111. low = 3;
  112. high = 7;
  113. int group=0, counter=0;
  114.  
  115. scanf ("%d", &group);
  116. while (counter == 0) {
  117. if (group >= 3 && group <= 7) {
  118. printf ("Thank You!\n");
  119. counter++;
  120. }
  121. else {
  122. printf ("***Error! You entered '%d' ... Value must be between 3 and 7 inclusive!***\n", group);
  123. printf ("Enter a value (between 3 and 7) : ");
  124. scanf ("%d", &group);
  125. }
  126. }
  127.  
  128. return group;
  129. }
  130.  
  131. int genRand(int limit) {
  132. int randomNum = rand();
  133.  
  134. while (randomNum >= limit) {
  135. randomNum = rand();
  136. }
  137. return randomNum;
  138. }
  139.  
  140. int validatePick(int pick, int first, int second, int third, int fourth, int fifth, int sixth, int seventh){
  141. if (pick == first && first != -1)
  142. return 0;
  143. else if (pick == second && second != -1)
  144. return 0;
  145. else if (pick == third && third != -1)
  146. return 0;
  147. else if (pick == fourth && fourth != -1)
  148. return 0;
  149. else if (pick == fifth && fifth != -1)
  150. return 0;
  151. else if (pick == sixth && sixth != -1)
  152. return 0;
  153. else if (pick == seventh && seventh != -1)
  154. return 0;
  155. else
  156. return 1;
  157.  
  158. }
  159.  
  160. void printNums (int n, int first, int second, int third, int fourth, int fifth, int sixth, int seventh) {
  161. printf ("Numbers Generated: %d\n", n); //Delete after Testing
  162. printf ("%d, %d, %d", first, second, third);
  163.  
  164. if (n == 4)
  165. printf (", %d\n", fourth);
  166. else if (n == 5)
  167. printf (", %d, %d\n", fourth, fifth);
  168. else if (n == 6)
  169. printf (", %d, %d, %d\n", fourth, fifth, sixth);
  170. else if (n == 7)
  171. printf (", %d, %d, %d, %d\n", fourth, fifth, sixth, seventh);
  172.  
  173. }
  174.  
  175. //= n! / ((n-k)! * k!)
  176. //odds = fun1 / (fun2 * fun3)
  177.  
  178. double calcOdds (int n, int k) { //n = largest k = set
  179. int i, nk;
  180. double odds, fun23, fun1 = 1, fun2 = 1, fun3 = 1;
  181.  
  182. nk = n - k;
  183.  
  184. printf ("n = %d\n", n);
  185. printf ("k = %d\n", k);
  186. printf ("nk = %d\n", nk);
  187.  
  188.  
  189. for (i = 1; i <= n; i++)
  190. fun1 = fun1 * i;
  191. for (i = 1; i <= nk; i++)
  192. fun2 = fun2 * i;
  193. for (i = 1; i <= k; i++)
  194. fun3 = fun3 * i;
  195.  
  196. fun23 = fun2 * fun3;
  197. odds = fun1 / (fun2 * fun3);
  198.  
  199. printf ("odds: %f", odds);
  200.  
  201. return odds;
  202.  
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement