Skip to content

Instantly share code, notes, and snippets.

@fejesjoco
Last active August 2, 2017 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fejesjoco/1de6723c2ae36f5c277efa0f69c7a91f to your computer and use it in GitHub Desktop.
Save fejesjoco/1de6723c2ae36f5c277efa0f69c7a91f to your computer and use it in GitHub Desktop.
/*
* Feladat:
* Irjon C programot az otoslotto nyeroszamainak veletlenszeru eloallitasara!
* Ugyeljen, hogy ne ismetlodjenek a szamok!
*
* Megoldas:
* Legeneralunk a "szamok" tombbe egy szamsort 1-90-ig (lehetseges nyeroszamok),
* Ezekbol kihuzunk egy szamot, amit beirunk a "nyeroszamok" tombbe,
* A kihuzott szamot a "szamok" tombben kicsereljuk az utolso elemmel,
* Legkozelebb a "szamok" tomb elsotol utolso elotti elemeig valasztunk nyeroszamot,
* A kihuzott szamot a "szamok" tombben kicsereljuk az utolso elotti elemmel,
* Legkozelebb a "szamok" tomb elsotol utolso elotti elotti elemeig valasztunk nyeroszamot...
* ...es igy tovabb.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int huzasok = 5;
int nyeroszam;
int i;
int ismetles;
int szamok[90];
int index;
int nyeroszamok[huzasok];
srand(time(NULL));
for (ismetles = 0; ismetles < 30000; ismetles++) {
// kihuzhato szamok tombje (1-90)
for (i = 0; i < 90; i++) {
szamok[i] = i + 1;
}
for (i = 0; i < huzasok; i++) {
// random index valasztasa egyre kisebb halmazbol, egyenletes eloszlassal
index = (int) (rand() / (1.0 + RAND_MAX) * (90 - i));
nyeroszamok[i] = szamok[index];
// a kihuzott szam helyere az utolsot tesszuk
szamok[index] = szamok[89 - i];
}
for (i = 0; i < huzasok; i++) {
printf("%d%s", nyeroszamok[i], i == huzasok - 1 ? "\n" : "\t");
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment