Answers:
您可能需要print
用alert
或替换其他内容,具体取决于您的JavaScript环境。
for(C=L=T=0;C++<10;print(L=T))while(!(T-L&&T+L-7))T=Math.random()*6+1|0
重击
#/!bin/bash
f=`expr $RANDOM % 6`
f=`expr $f + 1`
printf "$f"
for ((i=0; i<9; i++))
do
((bad=7-$f))
next=`expr $RANDOM % 6`
next=`expr $next + 1`
while [ $next -eq $bad ] || [ $next -eq $f ]
do
next=`expr $RANDOM % 6`
next=`expr $next + 1`
done
printf "$next"
f=$next
done
示例代码:http : //ideone.com/CCfro
((var=expression))
非常好-我认为最短的方法是var=$((expression))
但是为什么您只使用一次,而在反引号中的expr上浪费大量的字符呢?
for(i in 0:9)cat(F<-sample(setdiff(1:6,c(F,7-F)),1))
for((i = 10,f = RANDOM%6 + 1; i--;))做 printf $ f (((n = RANDOM%4 + 1,m = f <4?f:7-f,f = n <m || ++ n <7-m?n:n + 1)) 做完了
关键思想是要在[1,x]中选择一个不等于y的随机数,您可以在[1,x-1]中选择一个随机数,然后在> = y时递增。对于这个问题,我们想要[1,6]中的随机数不等于f或7-f。我们必须按照min(f,7-f),max(f,7-f)的顺序执行两个测试。
假设最初为空的环境可以通过不初始化i并将循环条件更改为2来节省2个字符 i++<10
从Aman ZeeK Verma的答案中获得大量高尔夫机会:
为((i = 10,f = 0; i--;))做 for((n = f; n == f || n + f == 7; f = RANDOM%6 + 1))做: 做完了 printf $ f 做完了
注意,可以通过将第一行更改为5个字符来缩小它,for((;i++<10;))
但这使假设并不总是正确的。在ideone中它可以正常工作,但是从shell运行它的人可以拥有i
或f
导出到非零值。
man bash
但是,如果您有时间阅读,我建议您阅读。我曾经读过本书的封面,只是对可能和值得查找的内容有一个模糊的想法对我很有帮助。
-join(0..9|%{($d=1..6-ne(7-$d)-ne$d|random)})
实际上,这很简单。我生成了可能掷骰子的列表,1..6
然后仅选择不等于7减去最后掷骰子的骰子,然后仅选择不等于最后掷骰子的骰子。然后,从其余列表中选择一个随机项目并将其分配给$d
。由于$d
最初是0
在第一次掷出普通骰子时进行处理的。
测试脚本:
for($i=0;$i-lt20;$i++){
$o=@(./tipping.ps1)
if ($i-gt0-and$o-eq$o2) { throw "Must have random output" }
if ($o.count-ne1) { throw "Must only have one line of output" }
if ($o[0]-match'[^1-6]'){ throw "Invalid characters" }
if($o[0].length-ne10){ throw "Wrong length: $($o[0].length)" }
$r=[char[]]($o[0])|%{$_-48}
for ($x=1;$x-lt$r.count;$x++){
if ($r[$x-1]+$r[$x]-eq7) { throw "Not a tipping: $($r[$x-1]) and $($r[$x])" }
}
$o2=$o
}
历史:
The term 'random' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.
Get-Random
cmdlet在v1中不存在。
(0..9).reduce([]){|m|m<<((1..6).to_a-[d=m[-1]||0,7-d]).shuffle[0]}
>:(?@4:{(i.6)-.],5&-)^:(<10)?6
6 2 3 5 4 2 4 1 3 6
说明(从右到左阅读):
?6
返回0到5之间的随机数^:(<10)
将函数应用9次,一路累加结果。该函数是:?@4:{(i.6)-.],5&-
] , 5&-
返回输入数字及其补数为5的数组(我们当前正在处理从0开始的数字,所以相反的面孔之和为5)(i. 6) -.
从完整的整数0到5中删除它们。从输入位置进行一次小费操作后,我们将获得所有有效位置。?@4: {
随机选择其中之一。>:
增加整个序列,使数字回到1到6的间隔。16 2f 25 08 41 20 17 30 16 2f 31 31 25 09 19 32
运作方式如下
16 2f 25 # make range from 1 to 6 and push random element
08 # start block
41 # duplicate top of stack twice
20 17 30 # negate top of stack and add 7
16 2f # push range from 1 to 6
31 31 # do set-wise difference with each of the two previous numbers
25 # push a random element from the list
09 # end block
19 32 # repeat block 9 times
For(I,1,9
Ans→X
Repeat Ans≠X and Ans≠7-X
randInt(1,6
End
Disp Ans
End
无聊的解决方案,但比以前的版本短。我利用了一个事实,即在新的计算器上将其Ans
初始化为零。
v->{int d=(int)(Math.random()*6+1),i=10,p;String r=""+d;for(;i-->0;r+=d)for(p=d;p==d|p+d==7;d=(int)(Math.random()*6+1));return r;}
作为具有冗长主方法的完整程序,这将是178个字节:
interface M{static void main(String[]a){int d=(int)(Math.random()*6+1),i=10,p;String r=""+d;for(;i-->0;r+=d)for(p=d;p==d|p+d==7;d=(int)(Math.random()*6+1));System.out.print(r);}}
说明:
v->{ // Method with empty unused parameter and String return-type
int d=(int)(Math.random()*6+1),
// Random dice-roll 1-6
i=10, // Counter-integer, starting at 10
p; // Temp integer to store new side
String r=""+d; // Result-String, starting at the first dice-roll
for(;i-->0; // Loop (1) 10 times:
r+=d) // After every iteration, append the result with a random side
for(p=d; // Set the new side to the current side
p==d // Loop (2) as long as the new side and current side are the same
|p+d==7; // or as long as both combined are exactly 7:
d=(int)(Math.random()*6+1)
// Set the new side to a random side 1-6
); // End of loop (2)
// End of loop (1) (implicit / single-line body)
return r; // Return the result-String
} // End of method
a=randi(6)
for i=1:9;b=1:6;b([a,7-a])=[];a=b(randi(4))
end
c(3,5,1,4,2,6)[(sample(1:6,1)+cumsum(sample((-2:2)[-3],9,T)))%%6+1]
有一个高尔夫球手的R答案,但我认为这与迄今为止提交的答案不同。
c(3,5,1,4,2,6) #A dice and its facets
(sample(1:6,1) #Initial dice roll
+cumsum(sample((-2:2)[-3],9,T))) #9 tippings in c(-2,-1,1,2)
%%6+1 #converts to values in [0,6]
[ ]#
6LΩUTFX?6LʒDXÊsX+7Ê*}ΩU
绝对可以打高尔夫球,但我目前没有看到。
说明:
6LΩ # Pick a random value from the range [1,6]
# i.e. [1,2,3,4,5,6] → 3
U # Save this random value in variable `X`
TF # Loop 10 times:
X? # Print `X` without newline to STDOUT
6Lʒ } # Create a range [1,6] again, and filter it by:
DXÊ # Check if the current value is not equal to `X`
# i.e. 1 and 3 → 1 (truthy)
# i.e. 3 and 3 → 0 (falsey)
sX+ # Sum the current value with `X`
# i.e. 1 and 3 → 4
# i.e. 3 and 3 → 6
7Ê # And check if it's not equal to 7
# i.e. 4 and 7 → 1 (truthy)
# i.e. 6 and 7 → 1 (truthy)
* # If both checks are truthy, keep it in the filtered list
# i.e. 1 and 1 → 1 (truthy)
# i.e. 0 and 1 → 0 (falsey)
Ω # Pick a random value from the filtered list
# i.e. [1,2,5,6] → 1
U # And save it in variable `X` for the next iteration of the loop