Skip to content

Instantly share code, notes, and snippets.

@Elevista
Last active March 13, 2016 00:12
Show Gist options
  • Save Elevista/ab0b54d275f2e10773fe to your computer and use it in GitHub Desktop.
Save Elevista/ab0b54d275f2e10773fe to your computer and use it in GitHub Desktop.
Lotto generate algorithm (C,C++,Java,Javascript,Python)

##Lotto generate algorithm

#include <vector>
#include <algorithm>
#include <ctime>
#include <stdio.h>
using namespace std;
int main() {
srand((unsigned int) time(NULL));
vector<int> num;
for (int i = 1; i <= 45; i++)
num.push_back(i); //1~45로 초기화
random_shuffle(num.begin(), num.end()); //모두 섞음
sort(num.begin(), num.begin() + 6); //앞 6개 숫자만 정렬
for (int n : vector<int>(num.begin(), num.begin() + 6))
printf("%02d ", n); //표시
return 0;
}
#include <vector>
#include <algorithm>
#include <random>
#include <chrono>
#include <stdio.h>
using namespace std;
int main() {
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
mt19937_64 rnd(seed);
vector<int> num;
for (int i = 1; i <= 45; i++)
num.push_back(i); //1~45로 초기화
shuffle(num.begin(), num.end(), rnd); //모두 섞음
sort(num.begin(), num.begin() + 6); //앞 6개 숫자만 정렬
for (int n : vector<int>(num.begin(), num.begin() + 6))
printf("%02d ", n); //표시
return 0;
}
console.log(_(_.range(1,46)).sampleSize(5).sortBy()+'');
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
int comp(const void *a, const void *b) {
int v = *(int*) a - *(int*) b;
return v ? v > 0 ? 1 : -1 : 0;
}
void random_shuffle(int arr[], int size) {
int i;
for (i = size - 1; i > 0; i--)
swap(&arr[i], &arr[rand() % (i + 1)]);
}
int main(void) {
int num[45], i;
srand(time(NULL));
for (i = 0; i < 45; i++)
num[i] = i + 1; //1~45로 초기화
random_shuffle(numbers, 45); //모두 섞음
qsort(num, 6, sizeof(int), comp); //앞 6개 숫자만 정렬
for (i = 0; i < 6; i++)
printf("%02d ", num[i]); //표시
return 0;
}
import java.util.*;
public class LottoGen {
public static void main(String[] args) {
List<Integer> num = new Vector<Integer>(45);
for (int i = 1; i <= 45; i++)
num.add(i); // 1~45로 초기화
Collections.shuffle(num); //모두 섞음
List<Integer> selected = num.subList(0, 6); // 앞 6개 숫자만 정렬
Collections.sort(selected);
for (int n : selected)
System.out.format("%02d ", n); // 표시
}
}
function swap(arr, a, b) {
var temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
function random_shuffle(arr) {
for (var i = arr.length - 1; i > 0; i--)
swap(arr, i, Math.floor(Math.random() * (i + 1)));
}
(function() {
var num = [];
for (var i = 1; i <= 45; i++)
num.push(i); // 1~45로 초기화
random_shuffle(num); //모두 섞음
var selected = num.slice(0, 6); //앞 6개 숫자만 정렬
selected.sort(function(a,b){return a-b;});
console.log(selected); // 표시
})();
import random
selected = random.sample(range(1, 46), 6) #1~45중 숫자 6개 뽑음
selected.sort() #정렬
print(selected) #표시
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment