Pseudorandom number generator image: Difference between revisions

→‎{{header|Wren}}: Updated preamble and changed to Wren S/H
m (syntax highlighting fixup automation)
(→‎{{header|Wren}}: Updated preamble and changed to Wren S/H)
 
(7 intermediate revisions by 5 users not shown)
Line 45:
Output can be seen by copying/pasting the above code [https://skilldrick.github.io/easy6502/ here.]
 
=={{header|Ada}}==
<syntaxhighlight lang="ada">
-- Generate a (pseudo)random image
-- J. Carter 2023 Apr
-- Uses Ada_GUI (https://github.com/jrcarter/Ada_GUI)
 
with Ada.Numerics.Discrete_Random;
with Ada_GUI;
 
procedure Random_Image is
package Color_Random is new Ada.Numerics.Discrete_Random (Result_Subtype => Ada_GUI.RGB_Value);
Gen : Color_Random.Generator;
Image : Ada_GUI.Widget_ID;
Event : Ada_GUI.Next_Result_Info;
use type Ada_GUI.Event_Kind_ID;
begin -- Random_Image
Color_Random.Reset (Gen => Gen);
Ada_GUI.Set_Up (Title => "Random Image");
Image := Ada_GUI.New_Graphic_Area (Width => 250, Height => 250);
All_X : for X in 0 .. 249 loop
All_Y : for Y in 0 .. 249 loop
Image.Set_Pixel (X => X, Y => Y, Color => (Red => Color_Random.Random (Gen),
Green => Color_Random.Random (Gen),
Blue => Color_Random.Random (Gen),
Alpha => 1.0) );
end loop All_Y;
end loop All_X;
Wait : loop
Event := Ada_GUI.Next_Event;
exit Wait when not Event.Timed_Out and then Event.Event.Kind = Ada_GUI.Window_Closed;
end loop Wait;
Ada_GUI.End_GUI;
end Random_Image;
end.</syntaxhighlight>
 
{{out}}
[[Media:Random_Image.png]]
 
=={{header|Delphi}}==
Line 368 ⟶ 410:
}
</syntaxhighlight>
 
=={{header|jq}}==
'''Works with jq and gojq, the C and Go implementations of jq'''
 
It has been claimed that the elementary cellular automaton with "Rule 30" can
be used as a PRNG,
(see e.g. [[Elementary_cellular_automaton/Random_number_generator]]),
so this entry generates a set of (x,y,color) co-ordinates so that this
hypothesis might be visually evaluated e.g. using the gnuplot program.
 
To keep things brief, the jq filter definitions at
[[Elementary_cellular_automaton#jq]] are used but not repeated here.
<syntaxhighlight lang=jq>
include "elementary-cellular-automaton" {search : "."}; # the defs at [[Elementary_cellular_automaton#jq]]
 
def binary2number:
reduce (.[]|tonumber) as $x ({p:1}; .n += .p * $x | .p *= 2) | .n;
 
# Emit a stream of $n PRNGs in range(0;255)
def prng($n):
# 30 is 11110
("1" + 100 * "0" )
| [automaton(30; 8 * $n) | .[0:1]]
| _nwise(8) | binary2number ;
 
foreach prng(99*99) as $color ({x:0, y:1};
.color = $color
| .x += 1
| if .x == 100 then .x = 1 | .y += 1 else . end )
| "\(.x) \(.y) \(.color)"
</syntaxhighlight>
Invocation:
<pre>
jq -nrf program.jq > prng.txt
gnuplot
plot("prng.txt") with image pixels
</pre>
 
=={{header|Julia}}==
Line 413 ⟶ 492:
writePgm(img, "prng_img.pgm", string.format("PRNG Image (%d x %d)", size, size))</syntaxhighlight>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
genmatrix(lambda([i,j],random(1000)),1000,1000)$
wxdraw2d(image(%,0,0,30,30));
</syntaxhighlight>
[[File:PseudoRandomImageMaxima.png|thumb|center]]
 
=={{header|Nim}}==
Line 437 ⟶ 522:
use GD;
 
my $img = new GD::Image->new(500, 500, 1);
 
for my $y (0..500) {
for my $x (0..500) {
my $color = $img->colorAllocate(rand 256, rand 256, rand 256);
$img->setPixel($x, $y, $color);
Line 479 ⟶ 564:
(prin (if (rand T) 1 0)) )
(prinl) ) )</syntaxhighlight>
=={{header|Python}}==
'''Libraries:''' [https://pypi.org/project/Pillow/ Pillow], [https://docs.python.org/3/library/random.html random]<syntaxhighlight lang="python3">
# pseudorandom number image generator by Xing216
from random import randbytes
from PIL import Image
 
size = 1500
x = bytes.fromhex(" ".join([randbytes(3).hex() for x in range(size*size)]))
img = Image.frombuffer('RGB', (size, size), x, 'raw', 'RGB', 0, 1)
img.show()
</syntaxhighlight>'''Output:''' [https://transfer.sh/tyN5SS95M0/rcXing216.png rcXing216.png] (transfer.sh)
 
=={{header|Raku}}==
Line 521 ⟶ 617:
=={{header|Wren}}==
{{libheader|DOME}}
WrenDOME's 'random' module uses thea '[https://en.wikipedia.org/wiki/Well_equidistributed_long-period_linearpseudorandom Wellnumber equidistributedgenerator long-periodbased on the linear]'''Squirrel3''' (WELL512aoptionally '''Squirrel5''') PRNGnoise function which doesn't need to be seeded with a prime number. It is in fact seeded from a sequence of 16 numbers but, if less are provided, the others are generated automatically. Typically (as here) the seed is generated from the current system time.
<syntaxhighlight lang="ecmascriptwren">import "dome" for Window
import "graphics" for Canvas, Color
import "random" for Random
9,479

edits