Created
November 5, 2009 09:26
-
-
Save tomohiro/226929 to your computer and use it in GitHub Desktop.
ロト6 の番号をランダムに選択するプログラムをあらゆる言語で実装
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package { | |
import flash.display.Sprite; | |
import flash.text.TextField; | |
public class Loto6 extends Sprite { | |
public function Loto6() { | |
var textField:TextField = new TextField(); | |
textField.text = range(1, 43).shuffle().slice(0, 6).join('-'); | |
addChild(textField); | |
} | |
private function range(first:int, last:int):Array { | |
var list:Array = []; | |
for (var i:int = first; i <= last; i++) { | |
list.push(i); | |
} | |
return list; | |
} | |
Array.prototype.shuffle = function():Array { | |
var i:int = this.length; | |
while (--i) { | |
var j:int = Math.floor(Math.random() * i); | |
var t:int = this[i]; | |
this[i] = this[j]; | |
this[j] = t; | |
} | |
return this; | |
}; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env awk -f | |
BEGIN { | |
range(numbers, 1, 43); | |
shuffle(numbers); | |
take(numbers, 6); | |
print join(numbers, "-"); | |
} | |
function range(list, first, last) { | |
for (i = first; i <= last; i++) { | |
list[i] = i; | |
} | |
} | |
function shuffle(list) { | |
srand(); | |
for (i = size(list); i >= 1; i--) { | |
j = int((i + 1) * rand()); | |
if (j == 0) { | |
j = 1; | |
} | |
tmp = list[i]; | |
list[i] = list[j]; | |
list[j] = tmp; | |
} | |
} | |
function size(list) { | |
counter = 0; | |
for (key in list) { | |
counter++; | |
} | |
return counter; | |
} | |
function take(list, count) { | |
counter = 0; | |
for (key in list) { | |
if (count <= counter) { | |
delete list[key]; | |
} | |
counter++; | |
} | |
} | |
function join(list, delimiter) { | |
for (key in list) { | |
if (length(composition) == 0) { | |
composition = list[key]; | |
} else { | |
composition = composition delimiter list[key]; | |
} | |
} | |
return composition; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <math.h> | |
main() | |
{ | |
int numbers[43]; | |
int i, j, tmp; | |
for (i = 0; i < 43;) { | |
numbers[i] = ++i; | |
} | |
srand(time(NULL) + time(NULL)); | |
for (i = 0; i < 43; i++) { | |
j = rand() % 43; | |
tmp = numbers[j]; | |
numbers[j] = numbers[i]; | |
numbers[i] = tmp; | |
} | |
printf("%d-%d-%d-%d-%d-%d\n", numbers[0], numbers[1], numbers[2], numbers[3], numbers[4], numbers[5]); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
class Loto6 | |
{ | |
static void Main(string[] args) | |
{ | |
List<int> numbers = Enumerable.Range(1, 43).OrderBy(i => Guid.NewGuid()).Take(6).ToList(); | |
Console.Write(string.Join("-", numbers.ConvertAll<string>(delegate(int i) { return i.ToString(); }).ToArray())); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% $ erl | |
% 1> c(loto6). | |
% {ok, loto6} | |
% 2> loto6:main(). | |
-module(loto6). | |
-compile([export_all]). | |
main() -> | |
io:format( | |
string:join( | |
lists:map( | |
fun(N) -> integer_to_list(N) end, | |
lists:sublist(shuffle(lists:seq(1, 43)), 6) | |
), "-") | |
). | |
% http://www.erlang.org/doc/man/random.html | |
% http://en.literateprograms.org/Fisher-Yates_shuffle_%28Erlang%29 | |
shuffle(List) -> shuffle(List, []). | |
shuffle([], List) -> List; | |
shuffle(List, Acc) -> | |
{L, [H | T]} = lists:split(random:uniform(length(List)) - 1, List), | |
shuffle(L ++ T, [H | Acc]). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' make by http://twitter.com/ekyan, http://twitter.com/kubio | |
VERSION 5.00 | |
Begin VB.Form Form1 | |
Caption = "Form1" | |
ClientHeight = 3195 | |
ClientLeft = 60 | |
ClientTop = 345 | |
ClientWidth = 4680 | |
LinkTopic = "Form1" | |
ScaleHeight = 3195 | |
ScaleWidth = 4680 | |
StartUpPosition = 3 'Windows ‚ÌŠù’è’l | |
Begin VB.CommandButton Command1 | |
Caption = "Command1" | |
Height = 615 | |
Left = 2040 | |
TabIndex = 1 | |
Top = 1200 | |
Width = 1695 | |
End | |
Begin VB.TextBox Text1 | |
Height = 735 | |
Left = 120 | |
TabIndex = 0 | |
Top = 120 | |
Width = 3615 | |
End | |
End | |
Attribute VB_Name = "Form1" | |
Attribute VB_GlobalNameSpace = False | |
Attribute VB_Creatable = False | |
Attribute VB_PredeclaredId = True | |
Attribute VB_Exposed = False | |
Option Explicit | |
Private Sub Command1_Click() | |
Text1.Text = Join(ConvertIntArrayToStrArray(Take(Shuffle(Range(1, 43)), 6)), "-") | |
End Sub | |
Private Function Range(ByVal first As Integer, ByVal last As Integer) As Integer() | |
Dim i As Integer | |
Dim listSize As Integer | |
Dim list() As Integer | |
listSize = last - first + 1 | |
ReDim list(listSize - 1) | |
For i = 0 To listSize - 1 | |
list(i) = i + first | |
Next i | |
Range = list | |
End Function | |
Private Function Shuffle(ByRef list() As Integer) As Integer() | |
Dim refList() As Integer | |
Dim j As Integer, tmp As Integer, k As Integer | |
Dim listSize As Integer | |
refList = list | |
listSize = UBound(list) + 1 | |
j = listSize - 1 | |
While (j > 0) | |
k = Int(Math.Rnd() * listSize) | |
tmp = refList(j) | |
refList(j) = refList(k) | |
refList(k) = tmp | |
j = j - 1 | |
Wend | |
Shuffle = refList | |
End Function | |
Private Function Take(ByRef list() As Integer, ByVal takeSize As Integer) As Integer() | |
Dim retList() As Integer | |
Dim i As Integer | |
ReDim retList(takeSize - 1) | |
For i = 0 To takeSize - 1 | |
retList(i) = list(i) | |
Next | |
Take = retList | |
End Function | |
Private Function ConvertIntArrayToStrArray(ByRef list() As Integer) As String() | |
Dim retList() As String | |
Dim listSize As Integer | |
Dim i As Integer | |
listSize = UBound(list) + 1 | |
ReDim retList(listSize - 1) | |
For i = 0 To listSize - 1 | |
retList(i) = CStr(list(i)) | |
Next | |
ConvertIntArrayToStrArray = retList | |
End Function | |
Private Sub Form_Initialize() | |
Call Randomize | |
End Sub |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt"; | |
"rand"; | |
"time"; | |
"strconv"; | |
"strings"; | |
) | |
func main() { | |
numbers := make([]string, 43); | |
for i := 0; i < len(numbers); i++ { | |
numbers[i] = strconv.Itoa(i + 1); | |
} | |
rand.Seed(time.Seconds()); | |
for i, value := range numbers { | |
j := rand.Intn(i); | |
numbers[i], numbers[j] = numbers[j], value; | |
} | |
fmt.Println(strings.Join(numbers[0:6], "-")) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- $ ghc -e main loto6.hs | |
import System.Random | |
import Data.List | |
main = getStdGen >>= \g -> print $ intercalate "-" $ map show $ take 6 $ shuffle [1..43] g | |
-- http://mblog.excite.co.jp/user/devadjust/entry/detail/?id=7899875 | |
shuffle [] g = [] | |
shuffle xs g = x : shuffle rest g' | |
where | |
(n, g') = randomR (0, length xs - 1) g | |
(x, rest) = pick n xs | |
pick n xs = let (ys, p:zs) = splitAt n xs in (p, ys ++ zs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Loto6 { | |
public static void main(String[] args) { | |
List<String> numbers = range(1, 43); | |
Collections.shuffle(numbers); | |
System.out.println(join(numbers.subList(0, 6), "-")); | |
} | |
static List<String> range(int first, int last) { | |
List<String> list = new ArrayList<String>(); | |
for (int i = first; i <= last; i++) { | |
list.add(Integer.toString(i)); | |
} | |
return list; | |
} | |
static String join(List<String> list, String delimiter) { | |
StringBuilder builder = new StringBuilder(); | |
for (String s : list) { | |
if (0 < builder.length()) { | |
builder.append(delimiter); | |
} | |
builder.append(s); | |
} | |
return builder.toString(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function range(first, last) { | |
var list = []; | |
for (let i = first; i <= last; i++) { | |
list.push(i); | |
} | |
return list; | |
} | |
Array.prototype.shuffle = function() { | |
var i = this.length; | |
while (--i) { | |
var j = Math.floor(Math.random() * i); | |
var tmp = this[i]; | |
this[i] = this[j]; | |
this[j] = tmp; | |
} | |
return this; | |
} | |
document.write(range(1, 43).shuffle().slice(0, 6).join('-')); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env lua | |
function range(first, last) | |
list = {} | |
for i = first, last do | |
table.insert(list, i) | |
end | |
return list | |
end | |
function shuffle(list) | |
math.randomseed(os.time()) | |
local i= #list | |
while i > 1 do | |
local j = math.random(i) | |
list[i], list[j] = list[j], list[i] | |
i = i - 1 | |
end | |
return list | |
end | |
print(table.concat({ unpack(shuffle(range(1, 43)), 1, 6) }, '-')) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
数値リストとは配列 | |
43回,それに回数を配列追加して,数値リストを配列シャッフル | |
それの1つ目から6つを配列取り出して「-」で配列結合して表示 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int[] numbers = new int[43]; | |
int i, j, tmp; | |
for (i = 0; i < 43;) { | |
numbers[i] = ++i; | |
} | |
randomSeed(minute() * second() * millis()); | |
for (i = 0; i < numbers.length; i++) { | |
j = int(random(i, numbers.length)); | |
tmp = numbers[i]; | |
numbers[i] = numbers[j]; | |
numbers[j] = tmp; | |
} | |
println(join(nf(subset(numbers, 0, 6), 0), "-")); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
$numbers = range(1, 43); | |
shuffle($numbers); | |
echo implode(array_slice($numbers, 0, 6), '-'); | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use List::Util qw/shuffle/; | |
print join('-', (shuffle(1..43))[0..5]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import random | |
print '-'.join(map(str, (random.sample(range(1, 44), 6)))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env R -f | |
shuffle <- function(list) { | |
return(list[order(runif(length(list)))]) | |
} | |
numbers <- shuffle(1:43) | |
paste(numbers[1:6], collapse = "-") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
p (1..43).to_a.shuffle.take(6).join('-') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.util.Sorting.stableSort | |
print(stableSort[Int, Double](1 to 43, { x => Math.random }).take(6).mkString("-")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env gosh | |
(use srfi-1) | |
(use srfi-13) | |
(use gauche.sequence) | |
(print (string-join (map number->string (take (shuffle (iota 43 1)) 6)) "-")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
seq 1 43 | sort -R | head -n 6 | tr '\012' '-' | sed 's/-$//g' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment