Pseudorandom number generator image: Difference between revisions

→‎{{header|Wren}}: Updated preamble and changed to Wren S/H
(Added XPL0 example.)
(→‎{{header|Wren}}: Updated preamble and changed to Wren S/H)
 
(10 intermediate revisions by 8 users not shown)
Line 19:
*   Linear congruential generator [https://en.wikipedia.org/wiki/Linear_congruential_generator].
<br><br>
 
=={{header|6502 Assembly}}==
===6502js/easy6502===
The "hardware" gives us a memory-mapped port at address $00FE which contains a different random number every clock cycle. We can use this to write to video memory (there are only 16 colors so the top 4 bits of the random value are ignored.)
<syntaxhighlight lang="6502asm">define vramPtr $00
define vramPtrHi $01
main:
;we're guaranteed to start off with all registers zeroed.
STA vramPtr
LDA #$02
STA vramPtrHi
LDX #4
loop:
LDA $FE ;read a random byte from the port
STA (vramPtr),y
INY
BNE loop
INC vramPtrHi
DEX
bne loop
brk ;end program</syntaxhighlight>
 
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 24 ⟶ 93:
{{libheader| vcl.Graphics}}
{{libheader| Vcl.Imaging.PngImage}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Pseudorandom_number_generator_image;
 
Line 112 ⟶ 181:
bmp.Free;
 
end.</langsyntaxhighlight>
{{out}}
[https://ibb.co/d6hwX1Y randbitmap-rdo.png].
Line 119 ⟶ 188:
Factor's default PRNG is Mersenne Twister, but it can be easily swapped out for others like Drand, Xoroshiro, Blum Blum Shub, lagged Fibonnaci, system RNGs, and more.
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight lang="factor">USING: accessors images.testing images.viewer literals math
random sequences ;
 
Line 127 ⟶ 196:
${ size size } >>dim
size sq 3 * [ 256 random ] B{ } replicate-as >>bitmap
image-window</langsyntaxhighlight>
 
=={{header|Forth}}==
Line 133 ⟶ 202:
{{works with|gforth|0.7.3}}
Uses gforth random generator to create PBM portable pixmap image file.
<langsyntaxhighlight lang="forth">require random.fs
: prngimage
outfile-id >r
Line 146 ⟶ 215:
r> to outfile-id ;
 
prngimage</langsyntaxhighlight>
 
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">Windowtitle "Pseudorandom number generator image"
Dim As Integer w = 500, h = w, x, y
Screenres w, h, 16
Line 160 ⟶ 229:
Next x
 
Bsave "Pseudo-Random-Algorithm.bmp",0</langsyntaxhighlight>
[https://www.dropbox.com/s/xsj4y5rnwlid6r4/Pseudo-Random-Algorithm.bmp?dl=0 image500.png] (sample image, offsite)
 
Line 168 ⟶ 237:
 
The image is saved to a .png file which can then be viewed with a utility such as EOG.
<langsyntaxhighlight lang="go">package main
 
import (
Line 200 ⟶ 269:
log.Fatal(err)
}
}</langsyntaxhighlight>
 
=={{header|Java}}==
Following implementation generates images from java.util.Random(uses linear congruential generator [https://en.wikipedia.org/wiki/Linear_congruential_generator].) and Blum Blum Shub Algorithm with least significant bit method and even bit parity method[https://en.wikipedia.org/wiki/Blum_Blum_Shub].
<syntaxhighlight lang="java">
<lang Java>
import javax.imageio.ImageIO;
import java.awt.*;
Line 340 ⟶ 409:
}
}
</syntaxhighlight>
</lang>
 
=={{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 346 ⟶ 452:
over 600 32-bit ints to represent its internal state, rather than just a product of
two or three primes.
<langsyntaxhighlight lang="julia">using FileIO, ImageIO
 
save("randombw.png", rand(Float16, 1000, 1000))
</syntaxhighlight>
</lang>
 
=={{header|Lua}}==
Lua uses the <code>xoroshiro256**</code> algorithm.
<langsyntaxhighlight Lualang="lua">size = 500
math.randomseed(os.time())
 
Line 384 ⟶ 490:
end
 
writePgm(img, "prng_img.pgm", string.format("PRNG Image (%d x %d)", size, size))</langsyntaxhighlight>
 
=={{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}}==
Nim standard PRNG is an implementation of the <code>xoroshiro128+</code> (xor/rotate/shift/rotate) algorithm which is extremely fast. The standard library provides a Mersenne Twister implementation too. For this task, we used the first one.
 
<langsyntaxhighlight Nimlang="nim">import random
import imageman
 
Line 402 ⟶ 514:
image[x, y] = color
 
image.savePNG("prng_image.png", compression = 9)</langsyntaxhighlight>
 
=={{header|Perl}}==
Perl unified the PRNG with [https://github.com/Perl/perl5/blob/11ab6d3b461cdc8944e706656209dd2a34f2f01d/util.c#L5793 its own internal drand48() implementation ] on all platforms since [https://perldoc.perl.org/5.20.0/perldelta.html#rand-now-uses-a-consistent-random-number-generator v5.20.0]. Without a manual srand, Perl by default source the seed from [https://github.com/Perl/perl5/blob/11ab6d3b461cdc8944e706656209dd2a34f2f01d/util.c#L4709 "/dev/urandom" ] if it is available so there shouldn't be any prime prerequisite.
<langsyntaxhighlight lang="perl">use strict;
use warnings;
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 420 ⟶ 532:
 
open F, "image500.png";
print F $img->png;</langsyntaxhighlight>
[https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/PNG-image500.png image500.png] (sample image, offsite)
 
=={{header|Phix}}==
{{libheader|Phix/pGUI}}
<!--<langsyntaxhighlight Phixlang="phix">(notonline)-->
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Pseudorandom_number_generator_image.exw</span>
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- IupSaveImage(), not possible from within a browser (though a "save" button might be?)</span>
Line 441 ⟶ 553:
<span style="color: #004080;">object</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">IupSaveImage</span><span style="color: #0000FF;">(</span><span style="color: #000000;">image</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"bw.png"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"PNG"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(seed (in "/dev/urandom" (rd 8)))
(out "image.pbm"
(prinl "P1")
Line 451 ⟶ 563:
(do 500
(prin (if (rand T) 1 0)) )
(prinl) ) )</langsyntaxhighlight>
=={{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}}==
MoarVM [https://github.com/MoarVM/MoarVM/blob/master/3rdparty/tinymt/tinymt64.c uses Mersenne Twister] as its PRNG but a prime seeder is not mandatory.
<syntaxhighlight lang="raku" perl6line># 20200818 Raku programming solution
 
use Image::PNG::Portable;
Line 469 ⟶ 592:
}
$o.write: "image$_.png" or die;
}</langsyntaxhighlight>
{{out}}
<pre>file image*.png
Line 481 ⟶ 604:
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">require('GD')
 
var img = %O<GD::Image>.new(500, 500, 1)
Line 490 ⟶ 613:
}
 
File("image500.png").write(img.png, :raw)</langsyntaxhighlight>
 
=={{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.
<langsyntaxhighlight ecmascriptlang="wren">import "dome" for Window
import "graphics" for Canvas, Color
import "random" for Random
Line 516 ⟶ 639:
 
static draw(dt) {}
}</langsyntaxhighlight>
 
=={{header|XPL0}}==
The PRNG is builtlinear incongruential toand theis languagebuilt-in. (as well as graphics) and isIt's seeded with the time-of-day.
<langsyntaxhighlight XPL0lang="xpl0">int X, Y;
[SetVid($11B); \VESA 1280x1024x24
for Y:= 0 to 1000-1 do
for X:= 0 to 1000-1 do
Point(X, Y, Ran($100_0000));
]</langsyntaxhighlight>
 
{{out}}
<pre>
SameEssentially the same as Delphi's image.
</pre>
9,476

edits