英文:
how to print values between 2 numbers in APL?
问题
我无法打印从30到50(包括30和50)的数值在APL中。
实际上,我尝试了很多运算符,但都没有奏效。
英文:
I am not able to print values ranging from 30 to 50 both inclusive in APL.
Actually I tried so much operators but not working.
答案1
得分: 2
以下是翻译好的部分:
你的约束是什么?你想定义一个函数吗,还是只想知道如何迭代?"print" 是指返回结果还是字面上打印到某个地方(输出/文件)?
在最简单的方式中,使用 iota ⍳
来迭代两个边界之间的距离,然后将较低的一个加到结果中。假设索引起点 IO
设置为 0
:
30 + ⍳21
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
如果 IO
设置为 1
,则减小较低的边界:
29 + ⍳21
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
您可以随时检查或设置 IO
,使用 ⎕IO
来切换:
⎕IO ← 0
0
英文:
What are your constraints? Do you want to define a function or do you just want to know how to iterate? Does "print" mean return as result or literally print to somewhere (output/file)?
In the easiest way, use iota ⍳
to iterate through the distance between the two bounds, then add the lower one to the results. Assuming index origin IO
is set to 0
:
30 + ⍳21
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
If IO
is set to 1
, lower the lower bound:
29 + ⍳21
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
You can always inspect or set IO
using ⎕IO
if you wanted to switch:
⎕IO ← 0
0
答案2
得分: 1
Sure, here is the translated content:
"实际上,我想要打印出在8到30之间的随机12个值。
如果⎕IO
设置为1
,你可以使用?23
(或?1+30-8
)生成大于或等于1
且小于或等于23
的随机整数。然后,将结果加上7
(或8-1
),它将在8到30之间,包括这两个值:7+?23
(如果⎕IO
设置为0
,生成的随机数将范围从0
到22
,因此要达到目标范围,你需要加上8
而不是7
,或者如果需要独立于⎕IO
的通用性,可以加上(8-⎕IO)
)。
现在,要从该范围内抽取多个随机数,请在其左参数中使用?
并提供你想要查看的抽取数量。结果将是一个具有相同长度的数组。
7+12?23
23 21 9 8 10 27 12 30 28 19 26 14
请注意,这些数字都是唯一的(没有重复)。如果要获得一个包含独立的随机数字的数组(值可以多次抽取),请使用⍴
来相应地调整?
的(右)参数,然后将其应用于整个数组:
7+?12⍴23
9 8 30 16 13 25 26 8 26 19 19 18
"
英文:
> Actually I wanted to print random 12 values between 8 to 30.
If ⎕IO
is set to 1
, you can generate a random integer larger or equal to 1
and smaller or equal to 23
using ?23
(or ?1+30-8
). Then, add 7
(or 8-1
) to the result, and it will be shifted between 8 and 30, both inclusive: 7+?23
. (With ⎕IO
set to 0
, the generated random number will instead range from 0
to 22
, so to reach the destination range you'd have to add 8
rather than 7
, or (8-⎕IO)
more generically if independence from ⎕IO
is a concern.)
Now, to draw more than one random number from that range, provide ?
in its left argument with the amount of draws you'd like to see. The result is an array of that length.
7+12?23
23 21 9 8 10 27 12 30 28 19 26 14
Note that these numbers are all unique (no duplicates). To instead get an array of independent random numbers (values can be drawn more than once), use ⍴
to accordingly reshape the (right) argument of ?
which will then be applied onto the whole array:
7+?12⍴23
9 8 30 16 13 25 26 8 26 19 19 18
答案3
得分: 0
@pmf's solution是简单的,但在处理两个大但接近的值时可能效率很低,并且如果任何一个数是负数,它会失败。
如果我们将边界称为⍺
和⍵
,那么⍺+¯1+⍳1+⍵-⍺
既高效又通用。如果⎕IO
为0,则移除¯1+
。您可以使用以下公式创建一个to
函数:
to←{(⍺-⎕IO)+¯1+⍳1+⍵-⍺}
30 to 50
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
或者,如果您使用Dyalog APL,您可以使用dfns工作区中的to
实用程序函数,如下所示:
'to' ⎕CY 'dfns'
30 to 50
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
英文:
@pmf's solution is simple, but can be very inefficient (for two large, but close values) and fails if any number is negative.
If we call the bounds ⍺
and ⍵
then ⍺+¯1+⍳1+⍵-⍺
is both efficient and general. Remove ¯1+
if ⎕IO
is 0. You can create a to
function using this formula as follows:
to←{(⍺-⎕IO)+¯1+⍳1+⍵-⍺}
30 to 50
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
Alternatively, if you use Dyalog APL, you can use the to
utility function from the dfns workspace, as follows:
'to' ⎕CY 'dfns'
30 to 50
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论