Jump to content

Pseudorandom number generator image: Difference between revisions

m
syntax highlighting fixup automation
No edit summary
m (syntax highlighting fixup automation)
Line 23:
===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.)
<langsyntaxhighlight lang="6502asm">define vramPtr $00
define vramPtrHi $01
main:
Line 41:
bne loop
brk ;end program</langsyntaxhighlight>
 
Output can be seen by copying/pasting the above code [https://skilldrick.github.io/easy6502/ here.]
Line 51:
{{libheader| vcl.Graphics}}
{{libheader| Vcl.Imaging.PngImage}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Pseudorandom_number_generator_image;
 
Line 139:
bmp.Free;
 
end.</langsyntaxhighlight>
{{out}}
[https://ibb.co/d6hwX1Y randbitmap-rdo.png].
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.
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight lang="factor">USING: accessors images.testing images.viewer literals math
random sequences ;
 
Line 154:
${ size size } >>dim
size sq 3 * [ 256 random ] B{ } replicate-as >>bitmap
image-window</langsyntaxhighlight>
 
=={{header|Forth}}==
Line 160:
{{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 173:
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 187:
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 195:
 
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 227:
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 367:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Julia}}==
Line 373:
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 411:
end
 
writePgm(img, "prng_img.pgm", string.format("PRNG Image (%d x %d)", size, size))</langsyntaxhighlight>
 
 
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.
 
<langsyntaxhighlight Nimlang="nim">import random
import imageman
 
Line 429:
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;
Line 447:
 
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 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: #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 478:
(do 500
(prin (if (rand T) 1 0)) )
(prinl) ) )</langsyntaxhighlight>
 
=={{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 496:
}
$o.write: "image$_.png" or die;
}</langsyntaxhighlight>
{{out}}
<pre>file image*.png
Line 508:
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">require('GD')
 
var img = %O<GD::Image>.new(500, 500, 1)
Line 517:
}
 
File("image500.png").write(img.png, :raw)</langsyntaxhighlight>
 
=={{header|Wren}}==
{{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.
<langsyntaxhighlight lang="ecmascript">import "dome" for Window
import "graphics" for Canvas, Color
import "random" for Random
Line 543:
 
static draw(dt) {}
}</langsyntaxhighlight>
 
=={{header|XPL0}}==
The PRNG is linear congruential and is built-in. It'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}}
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.