Constrained random points on a circle: Difference between revisions
Constrained random points on a circle (view source)
Revision as of 22:25, 13 February 2024
, 3 months ago→{{header|EasyLang}}
(implementation in Forth) |
|||
(9 intermediate revisions by 6 users not shown) | |||
Line 1,050:
end;
end.</syntaxhighlight>
=={{header|EasyLang}}==
[https://easylang.dev/show/#cod=bY0xDsIwFEP3nOKN0Igqv6hMzWGqtohIJYhPBMntUShsbLafZb8uYV2YYmJAnDNAxqNjnENMHIUDcqpx+R8rnsddE7tMQ8ZSaCj7ysIZcQweZYwzWqX0lcDn03rka6+350LvsNSdbpN1qvv1g07rsvVb05o3 Run it]
<syntaxhighlight>
while cnt < 100
x = randint 31 - 16
y = randint 31 - 16
r = sqrt (x * x + y * y)
if 10 <= r and r <= 15
cnt += 1
move 50 + x * 2 50 + y * 2
circle 1
.
.
</syntaxhighlight>
=={{header|EchoLisp}}==
Line 2,108 ⟶ 2,124:
</script></body></html></syntaxhighlight>
=={{header|jq}}==
{{works with|jq}}
This solution uses a "generate and test" approach to find exactly 100 points within the specified annulus.
Since jq does not have a built-in PRNG, /dev/random is used instead. gnuplot and a bash or bash-like environment are also assumed.
<syntaxhighlight lang="bash">
#!/bin/bash
< /dev/random tr -cd '0-9' | fold -w 1 | jq -Mcnr '
# Output: a PRN in range(0;$n) where $n is .
def prn:
if . == 1 then 0
else . as $n
| (($n-1)|tostring|length) as $w
| [limit($w; inputs)] | join("") | tonumber
| if . < $n then . else ($n | prn) end
end;
def ss: map(.*.) | add;
# Input: [x,y]
# Emit . iff ss lies within the given bounds
def annulus($min; $max) : ss as $sum | select($min <= $sum and $sum <= $max);
limit(100;
repeat([((30 | prn) - 15), ((30 | prn) - 15)]
| select( annulus(100; 225)) ))
| "\(.[0]) \(.[1])"
' > rc-annulus.dat
</syntaxhighlight>
The plot can now be generated as a .png file using these gnuplot commands:
<syntaxhighlight lang="bash">
reset
set terminal pngcairo
set output 'rc-annulus.png'
set xrange [-20:20]
set yrange [-20:20]
plot "rc-annulus.dat" with points pt 1
</syntaxhighlight>
=={{header|Julia}}==
Line 3,065 ⟶ 3,124:
47414232164
4 </syntaxhighlight>
=={{header|Quackery}}==
<syntaxhighlight lang="Quackery"> [ 0 31 of ] is grid ( --> [ )
[ dup * ] is squared ( n --> n )
[ squared swap squared +
10 squared 15 squared 1+
within ] is inrange ( n n --> b )
[ 32 random 16 -
32 random 16 -
2dup inrange not while
2drop again ] is randxy ( --> n n )
[ 15 + swap 15 +
dip [ 2dup peek ]
bit | unrot poke ] is plot ( [ n n --> [ )
[ witheach
[ 31 times
[ dup
i^ bit & iff
[ $ "[]" ]
else
[ $ " " ]
echo$ ]
drop
cr ] ] is draw ( [ --> )
[ grid
swap times
[ randxy plot ]
draw ] is circle ( n --> )
100 circle</syntaxhighlight>
{{out}}
<pre>
[] [] []
[] [] []
[] [][] []
[] [] [] [][]
[] [] [][][][] [] []
[]
[] []
[] [][]
[]
[][] []
[] [] [] []
[] []
[]
[] []
[][] []
[][] []
[] [][][]
[] []
[] [] [] []
[]
[] []
[] []
[][][] []
[] [] [][] [] []
[] [][][] [] [] []
[] [] []
[] [][]
[] []
</pre>
Code check as per task discussion, as a dialogue in the Quackery shell.
<pre>/O> 10000 circle
...
[]
[][][][][][][][][][][]
[][][][][][][][][][][][][][][]
[][][][][][][][][][][][][][][][][][][]
[][][][][][][][][][][][][][][][][][][][][]
[][][][][][][][][][][][][][][][][][][][][][][]
[][][][][][][][] [][][][][][][][]
[][][][][][][] [][][][][][][]
[][][][][][] [][][][][][]
[][][][][][] [][][][][][]
[][][][][][] [][][][][][]
[][][][][] [][][][][]
[][][][][] [][][][][]
[][][][][] [][][][][]
[][][][][] [][][][][]
[][][][][][] [][][][][][]
[][][][][] [][][][][]
[][][][][] [][][][][]
[][][][][] [][][][][]
[][][][][] [][][][][]
[][][][][][] [][][][][][]
[][][][][][] [][][][][][]
[][][][][][] [][][][][][]
[][][][][][][] [][][][][][][]
[][][][][][][][] [][][][][][][][]
[][][][][][][][][][][][][][][][][][][][][][][]
[][][][][][][][][][][][][][][][][][][][][]
[][][][][][][][][][][][][][][][][][][]
[][][][][][][][][][][][][][][]
[][][][][][][][][][][]
[]
Stack empty.
</pre>
=={{header|R}}==
Line 3,484 ⟶ 3,654:
[[File:CalmoSoftDrawCircle.jpg]]
=={{header|RPL}}==
{{works with|HP|48G}}
« ERASE -15 15 DUP2 XRNG YRNG <span style="color:grey">@ set graphics boundaries</span>
DEG -19 SF 0 <span style="color:grey">@ set degrees mode, polar vector mode, count = 0</span>
'''DO''' RAND 5 * 10 + RAND 360 * →V2 <span style="color:grey">@ z = rand(10..15).exp(i.rand(360))</span>
C→R IP SWAP IP R→C <span style="color:grey">@ make z a complex with integer coordinates</span>
'''IF''' DUP ABS DUP 10 ≥ SWAP 15 ≤ AND <span style="color:grey">@ if 10 ≤ | z | ≤ 15</span>
'''THEN''' PIXON 1 + <span style="color:grey">@ then set pixel and increment count</span>
'''ELSE''' DROP '''END'''
'''UNTIL''' DUP 100 ≥ '''END'''
DROP { } PVIEW
-19 CF
» '<span style="color:blue">TASK</span>' STO
[[File:Constrained.png|alt=Screenshot of a HP-48G emulator, displaying constrained random points on a circle|HP-48G emulator screenshot]]
The circle is actually an ellipse because RPL display screens are not squared.
=={{header|Ruby}}==
Line 3,772 ⟶ 3,959:
{{trans|Perl}}
Generates an EPS file.
<syntaxhighlight lang="ruby">var points = []
while (points.len < 100) {
var (x, y) = 2.of{
var r2 = (x**2 + y**2)
if ((r2 >= 100) && (r2 <= 225)) {
points.append([x, y])
}
}
print <<'HEAD'
%!PS-Adobe-3.0 EPSF-3.0
%%BoundingBox 0 0 400 400
Line 3,793 ⟶ 3,980:
HEAD
points.each { |pt| say "#{pt.join(' ')} pt" }
print '%%EOF'
=={{header|Standard ML}}==
Line 4,116 ⟶ 4,303:
=={{header|Wren}}==
{{libheader|DOME}}
<syntaxhighlight lang="
import "dome" for Window
import "random" for Random
|