スキップしてメイン コンテンツに移動

投稿

ラベル(Random)が付いた投稿を表示しています

SQLで特定の文字を組み合わせたランダムな文字列を生成

簡易的な方法として「指定した文字列からランダムに1文字選ぶ」を必要な文字の長さ分concat関数でつなげれば実現できます。 1文字ずつ文字を選ぶので、あまり性能もよくない上、セキュリティ的な観点からのランダム性も担保されていないので、あくまで開発中に必要になった時に使う程度が無難だと思います。 下記に英数字大文字小文字を含んだランダムな3文字の文字列を生成するクエリを示します。 # RAND関数で指定した文字列からランダムに1文字選択。 # 下記の例の62の部分はa~z、A~Z、1~9の文字数の合計値を入れた結果 SELECT CONCAT( SUBSTRING('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789', FLOOR(RAND() * 62 + 1), 1), SUBSTRING('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789', FLOOR(RAND() * 62 + 1), 1), SUBSTRING('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789', FLOOR(RAND() * 62 + 1), 1) ) AS random_string;

Java: Shuffling Elements in Array

I will show you java code for shuffling order of elements in array randomly. Only one thing you should notice is generating random is quite sensitive problem in a precise sense. I use java Random class which is provided by JDK. However if you need to use more proper randomness, you may implement class for generate random number sequence yourself. Anyway, the code is below. package com.dukesoftware.utils.math; import java.util.Random; public class Shuffler { private final Random random; public Shuffler(Random random) { this.random = random; } public void shuffle(int[] a) { for (int i = a.length - 1; i > 0; i--) { int j = random.nextInt(i + 1); // 0 <= j <= i int swap = a[i]; a[i] = a[j]; a[j] = swap; } } public void shuffle(double[] a) { for (int i = a.length - 1; i > 0; i--) { int j = random.nextInt(i + 1); // 0 <= j <= i double swap =