在 J lang 中使用较少的随机生成

Using less trivial random generation in J lang

我正在尝试使用 J 进行随机生成,并且会出现几个问题:

  1. 如果我想随机选择 10 个字符的词,我可以使用
   (?10 # 26) { 'abcdefghijklmnopqrstuvwxyz'
vqdffhrvso

从中选出 N 个的最佳方法是什么?例如三个10字的单词?

  1. 深入例子 1.非重复采摘怎么样?以上为重复采摘

  2. 我可以选择下面的4位数字

(?5 # 10) { '0123456789'
40400

(a) 如何潜入第一个不能为0的条件? (b) 例如我想从 <1234,1250> UNION (9999, 10002> 中选取自然数怎么说? (c) 如何在同一个集合中挑选实数?

  1. 延伸点3.知道概率权重分别为0.1、0.3、0.5、0.1,如何从1、3、6、10中随机挑选?

  2. 如何为任何分配选择号码?

提前感谢您的反馈!

What would be the best way to pick N of them?

这里有两种方式:

   ]alfa =: a. {~ (a.i.'a')+i.26
abcdefghijklmnopqrstuvwxyz
   rand =: alfa {~ ] ?@$ #@alfa
   rand 10
kuipaajyvm
   rand 3 10  NB. generate a 2d array of indices into alfa (rand called 1x)
zlqtjfcurj
odzdfcuqiy
mjmrylxpau
   rand&10"0 i.3  NB. generate three lists of indices (rand called 3x)
akfklfxuit
scqehljoah
pcptwgqdmm

How about non-repeating picking?

Dyadic ? 提供不重复。您在上面使用的是针对参数列表的 monadic ?

   sel =: alfa {~ (#alfa) ?~ ]
   sel&10"0 i.5
ysxagmekid
wbdqzkanum
wynmjuvcti
arhwqpegou
ofjigrctdn
   sel 30   NB. impossible
|domain error: sel
|       sel 30

How to sneak a condition that 0 cannot be the first digit?

  1. 将第一个数字与其余数字分开生成
  2. 不断生成答案,直到有人满足条件
   NB. option 1
   number =: 10 #. (>:@? bind 9) , (10 ?@$~ <:)
   number 3
212
   +/ 99 < (number bind 3)"0 i.1e4
10000

   NB. option 2
   number =: 10 #. 10 ?@$~ ]
   number 3
831
   +/ 99 < (number bind 3)"0 i.1e4  NB. some results with leading 0
8988

   NB. fold single, an infinite loop with a break
   goodnumber =: {{ (10^y-1)&{{y[1 Z: x <: y}} F. (number bind y) '' }}
   goodnumber 3
912
   +/99 < (goodnumber bind 3)"0 i.1e4  NB. no bad results
10000

probability weights

https://code.jsoftware.com/wiki/Fifty_Shades_of_J/Chapter_14

pick a number for any distribution

看看load 'stats/base/random'