Pseudorandom number generator image: Difference between revisions
Content added Content deleted
Puppydrum64 (talk | contribs) No edit summary |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 23: | Line 23: | ||
===6502js/easy6502=== |
===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.) |
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 |
define vramPtrHi $01 |
||
main: |
main: |
||
Line 41: | Line 41: | ||
bne loop |
bne loop |
||
brk ;end program</ |
brk ;end program</syntaxhighlight> |
||
Output can be seen by copying/pasting the above code [https://skilldrick.github.io/easy6502/ here.] |
Output can be seen by copying/pasting the above code [https://skilldrick.github.io/easy6502/ here.] |
||
Line 51: | Line 51: | ||
{{libheader| vcl.Graphics}} |
{{libheader| vcl.Graphics}} |
||
{{libheader| Vcl.Imaging.PngImage}} |
{{libheader| Vcl.Imaging.PngImage}} |
||
<syntaxhighlight lang="delphi"> |
|||
<lang Delphi> |
|||
program Pseudorandom_number_generator_image; |
program Pseudorandom_number_generator_image; |
||
Line 139: | Line 139: | ||
bmp.Free; |
bmp.Free; |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://ibb.co/d6hwX1Y randbitmap-rdo.png]. |
[https://ibb.co/d6hwX1Y randbitmap-rdo.png]. |
||
Line 146: | Line 146: | ||
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. |
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}} |
{{works with|Factor|0.99 2021-02-05}} |
||
< |
<syntaxhighlight lang="factor">USING: accessors images.testing images.viewer literals math |
||
random sequences ; |
random sequences ; |
||
Line 154: | Line 154: | ||
${ size size } >>dim |
${ size size } >>dim |
||
size sq 3 * [ 256 random ] B{ } replicate-as >>bitmap |
size sq 3 * [ 256 random ] B{ } replicate-as >>bitmap |
||
image-window</ |
image-window</syntaxhighlight> |
||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
Line 160: | Line 160: | ||
{{works with|gforth|0.7.3}} |
{{works with|gforth|0.7.3}} |
||
Uses gforth random generator to create PBM portable pixmap image file. |
Uses gforth random generator to create PBM portable pixmap image file. |
||
< |
<syntaxhighlight lang="forth">require random.fs |
||
: prngimage |
: prngimage |
||
outfile-id >r |
outfile-id >r |
||
Line 173: | Line 173: | ||
r> to outfile-id ; |
r> to outfile-id ; |
||
prngimage</ |
prngimage</syntaxhighlight> |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">Windowtitle "Pseudorandom number generator image" |
||
Dim As Integer w = 500, h = w, x, y |
Dim As Integer w = 500, h = w, x, y |
||
Screenres w, h, 16 |
Screenres w, h, 16 |
||
Line 187: | Line 187: | ||
Next x |
Next x |
||
Bsave "Pseudo-Random-Algorithm.bmp",0</ |
Bsave "Pseudo-Random-Algorithm.bmp",0</syntaxhighlight> |
||
[https://www.dropbox.com/s/xsj4y5rnwlid6r4/Pseudo-Random-Algorithm.bmp?dl=0 image500.png] (sample image, offsite) |
[https://www.dropbox.com/s/xsj4y5rnwlid6r4/Pseudo-Random-Algorithm.bmp?dl=0 image500.png] (sample image, offsite) |
||
Line 195: | Line 195: | ||
The image is saved to a .png file which can then be viewed with a utility such as EOG. |
The image is saved to a .png file which can then be viewed with a utility such as EOG. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 227: | Line 227: | ||
log.Fatal(err) |
log.Fatal(err) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Java}}== |
=={{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]. |
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 javax.imageio.ImageIO; |
||
import java.awt.*; |
import java.awt.*; |
||
Line 367: | Line 367: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
Line 373: | Line 373: | ||
over 600 32-bit ints to represent its internal state, rather than just a product of |
over 600 32-bit ints to represent its internal state, rather than just a product of |
||
two or three primes. |
two or three primes. |
||
< |
<syntaxhighlight lang="julia">using FileIO, ImageIO |
||
save("randombw.png", rand(Float16, 1000, 1000)) |
save("randombw.png", rand(Float16, 1000, 1000)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
Lua uses the <code>xoroshiro256**</code> algorithm. |
Lua uses the <code>xoroshiro256**</code> algorithm. |
||
< |
<syntaxhighlight lang="lua">size = 500 |
||
math.randomseed(os.time()) |
math.randomseed(os.time()) |
||
Line 411: | Line 411: | ||
end |
end |
||
writePgm(img, "prng_img.pgm", string.format("PRNG Image (%d x %d)", size, size))</ |
writePgm(img, "prng_img.pgm", string.format("PRNG Image (%d x %d)", size, size))</syntaxhighlight> |
||
Line 417: | Line 417: | ||
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. |
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. |
||
< |
<syntaxhighlight lang="nim">import random |
||
import imageman |
import imageman |
||
Line 429: | Line 429: | ||
image[x, y] = color |
image[x, y] = color |
||
image.savePNG("prng_image.png", compression = 9)</ |
image.savePNG("prng_image.png", compression = 9)</syntaxhighlight> |
||
=={{header|Perl}}== |
=={{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. |
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. |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use GD; |
use GD; |
||
Line 447: | Line 447: | ||
open F, "image500.png"; |
open F, "image500.png"; |
||
print F $img->png;</ |
print F $img->png;</syntaxhighlight> |
||
[https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/PNG-image500.png image500.png] (sample image, offsite) |
[https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/PNG-image500.png image500.png] (sample image, offsite) |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{libheader|Phix/pGUI}} |
{{libheader|Phix/pGUI}} |
||
<!--< |
<!--<syntaxhighlight lang="phix">(notonline)--> |
||
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Pseudorandom_number_generator_image.exw</span> |
<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> |
<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 468: | Line 468: | ||
<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: #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> |
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(seed (in "/dev/urandom" (rd 8))) |
||
(out "image.pbm" |
(out "image.pbm" |
||
(prinl "P1") |
(prinl "P1") |
||
Line 478: | Line 478: | ||
(do 500 |
(do 500 |
||
(prin (if (rand T) 1 0)) ) |
(prin (if (rand T) 1 0)) ) |
||
(prinl) ) )</ |
(prinl) ) )</syntaxhighlight> |
||
=={{header|Raku}}== |
=={{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. |
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. |
||
<lang |
<syntaxhighlight lang="raku" line># 20200818 Raku programming solution |
||
use Image::PNG::Portable; |
use Image::PNG::Portable; |
||
Line 496: | Line 496: | ||
} |
} |
||
$o.write: "image$_.png" or die; |
$o.write: "image$_.png" or die; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>file image*.png |
<pre>file image*.png |
||
Line 508: | Line 508: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Perl}} |
{{trans|Perl}} |
||
< |
<syntaxhighlight lang="ruby">require('GD') |
||
var img = %O<GD::Image>.new(500, 500, 1) |
var img = %O<GD::Image>.new(500, 500, 1) |
||
Line 517: | Line 517: | ||
} |
} |
||
File("image500.png").write(img.png, :raw)</ |
File("image500.png").write(img.png, :raw)</syntaxhighlight> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|DOME}} |
{{libheader|DOME}} |
||
Wren's 'random' module uses the '[https://en.wikipedia.org/wiki/Well_equidistributed_long-period_linear Well equidistributed long-period linear]' (WELL512a) PRNG 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 time. |
Wren's 'random' module uses the '[https://en.wikipedia.org/wiki/Well_equidistributed_long-period_linear Well equidistributed long-period linear]' (WELL512a) PRNG 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 time. |
||
< |
<syntaxhighlight lang="ecmascript">import "dome" for Window |
||
import "graphics" for Canvas, Color |
import "graphics" for Canvas, Color |
||
import "random" for Random |
import "random" for Random |
||
Line 543: | Line 543: | ||
static draw(dt) {} |
static draw(dt) {} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
The PRNG is linear congruential and is built-in. It's seeded with the time-of-day. |
The PRNG is linear congruential and is built-in. It's seeded with the time-of-day. |
||
< |
<syntaxhighlight lang="xpl0">int X, Y; |
||
[SetVid($11B); \VESA 1280x1024x24 |
[SetVid($11B); \VESA 1280x1024x24 |
||
for Y:= 0 to 1000-1 do |
for Y:= 0 to 1000-1 do |
||
for X:= 0 to 1000-1 do |
for X:= 0 to 1000-1 do |
||
Point(X, Y, Ran($100_0000)); |
Point(X, Y, Ran($100_0000)); |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |