Skip to content

Instantly share code, notes, and snippets.

@gofer
Last active December 1, 2017 13:25
Show Gist options
  • Save gofer/40d3c9a3df35b4fb8938e94e12c3bca9 to your computer and use it in GitHub Desktop.
Save gofer/40d3c9a3df35b4fb8938e94e12c3bca9 to your computer and use it in GitHub Desktop.
String Shuffle
/* curl https://gist.githubusercontent.com/gofer/40d3c9a3df35b4fb8938e94e12c3bca9/raw/shuffle.cc | g++ -xc++ -std=c++14 -Wall -O3 -o shuffle -; ./shuffle */
#include <iostream>
#include <algorithm>
#include <random>
#include <string>
int main()
{
::setlocale(LC_CTYPE, "");
std::wstring s = L"花金だーワッショーイ!テンションGAGEGAGEマック";
std::shuffle(s.begin(), s.end(), std::mt19937(std::random_device()()));
std::wcout << s << std::endl;
return 0;
}
// mcs -e '~~~'
using System;using System.Linq;public class shuffle{public static void Main(){var r=new Random();Console.WriteLine("花金だーワッショーイ!テンションGAGEGAGEマック".ToCharArray().OrderBy(x=>r.Next()).ToArray());}}
// node -e '~~~'
a="花金だーワッショーイ!テンションGAGEGAGEマック".split("");n=a.length;for(i=0;i<n-1;++i){(j=>a[j]=[a[i],a[i]=a[j]][0])(i+Math.floor(Math.random()*(n-i)))}console.log(a.join(""));
<?php
# $ php -r '~~~'
$_=preg_split("//u","花金だーワッショーイ!テンションGAGEGAGEマック",0,PREG_SPLIT_NO_EMPTY);shuffle($_);print(implode($_)."\n");
?>
# $ perl -E '~~~'
use Encode;use List::Util;say encode_utf8 join"",List::Util::shuffle split//,decode_utf8"花金だーワッショーイ!テンションGAGEGAGEマック"
# $ python -c '~~~'
import random;l=list("花金だーワッショーイ!テンションGAGEGAGEマック".decode("utf-8"));random.shuffle(l);print"".join(l)
# $ ruby -e '~~~'
puts "花金だーワッショーイ!テンションGAGEGAGEマック".split("").shuffle.join
(* echo '~~~' | sml *)
fun $(f,g)=f(g);infixr $;val g=Random.rand(0,LargeInt.toInt$(Time.toNanoseconds$Time.now())mod(LargeInt.fromInt$Option.valOf Int.maxInt));print$foldr(op^)"\\n"$map UTF8.encode$(fn l=>map(fn(a,b)=>a)$ListMergeSort.sort(fn((a,b),(c,d))=>b<d)$ListPair.zip(l,map(fn x=>Random.randRange(0,x)g)$List.tabulate(length l,fn x=>x)))$UTF8.explode"花金だーワッショーイ!テンションGAGEGAGEマック"
package info.ex_studio.dev.java;import java.util.Arrays;import java.util.Collections;import java.util.List;import java.util.stream.Stream;public class StringShuffle{public static void main(String[]args){List<Object>list=Arrays.asList("花金だーワッショーイ!テンションGAGEGAGEマック".chars().mapToObj(c->new Character((char)c).toString()).toArray());Collections.shuffle(list);System.out.println(list.stream().map(x->x.toString()).reduce(new String(""),(p,q)->p.concat(q)));}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment