Smooth Random Numbers

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
IcarusLives
Posts: 161
Joined: 17 Jan 2016 23:55

Smooth Random Numbers

#1 Post by IcarusLives » 27 Mar 2023 19:24

Hello everyone,

I working on a function to give me an array of smooth random numbers.

Code: Select all

@echo off & setlocal enableDelayedExpansion

set /a "wid=61", "hei=50"
mode !wid!,!hei!

set "esc="
set "pixel=Û"

set "map=(c)+((d)-(c))*((v)-(a))/((b)-(a))"

set "noise=!random:~-1!"
for /l %%i in (1,1,%wid%) do (
	set /a "seed=!noise:~-1!", "seed+=!random! %% 3 + -1"
	if !seed! leq 0 set "seed=0"
	if !seed! geq 9 set "seed=9"
	set "noise=!noise!!seed!"
)
set "noise[0]=!noise!"
for /l %%j in (1,1,%hei%) do (
	set /a "ly=%%j - 1"
	for %%l in (!ly!) do (
		for /l %%i in (0,1,%wid%) do (
			if "%%i" equ "0" (
				set /a "seed=!noise[%%l]:~0,1! + (!random! %% 3 + -1)"
			) else (
				set "lastXthisLine=!noise[%%j]:~-1!"
				set "lastYlastLine=!noise[%%l]:~%%i,1!"
				set /a "seed=(lastXthisLine + lastYlastLine + !random:~-1!) / 3 + 1"
			)
			if !seed! leq 0 set "seed=0"
			if !seed! geq 9 set "seed=9"
			set "noise[%%j]=!noise[%%j]!!seed!"
		)
	)
)

rem display
for /f "tokens=*" %%a in (map.txt) do (
	set "currLine=%%~a"
	set /a "totalLines+=1"
	for %%b in (!totalLines!) do (
		for /l %%c in (1,1,!wid!) do (
			set /a v=!noise[%%b]:~%%c,1!, a=0, b=9, c=0, d=255, "col=%map%"2>nul
			
				if /i "!currLine:~%%c,1!" equ "G" (
					<nul set /p "=%esc%[38;2;0;!col!;0m%pixel%"
				) else if /i "!currLine:~%%c,1!" equ "W" (
					<nul set /p "=%esc%[38;2;0;0;!col!m%pixel%"
				) else if /i "!currLine:~%%c,1!" equ "R" (
					<nul set /p "=%esc%[38;2;!col!;!col!;!col!m%pixel%"
				)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533454666455553343432345464564456455655576888
9778865565578856654445566767876576434545667566665757777555667
8786885675568675567744654575765777755465666546654677787675565
9877987766758655777756776575885768678576788756567865766875656
9777878866757667886877776776554789888878688768655667676555775
9878786888888765556777765667776886665576878676577855688886887
9988875568768676544666864466557687556566879865568964588886777
8867788656556765575678755455786787576657888966768765558778678
9967577557754765664666655788868789787655677866778876648889866
8686565557775775565677776779787579977887787777888688866778986
8654475756767866575565888787887677676898878667676568999688878
8646456577868768864456868665776586778877688675467567679666778
8554477767768666564335576557767778978768686765456678689667868
8864687756666686546436778567778656766786766675455447787677888
8887797665565787668644777665678657856687687675464466776657887
9796577655665787677655775457776766765576655465656667876766786
9997658765575576767677877756788678788865577654757788687785788
9997776577887556687886777576788788876667768576878865677675776
8665577655776568777878788755576567668658768578679996667756676
9775787765656548866665568875455547787778657576558877888877765
9676687764678767778865466685645677889658888577568998887765554
9877587776576666558667754457645778867547778866576776687577654
9887778766786786558655676475566676776776655777566676787675777
9886567786886667676555677687555686785566756665446654466765765
9999657686687878675654576786754777888567677767778775576754466
9889667577775765765786775666544677776557666857668785456877568
8999887556557677675877765567566776567775775666677875666657655
8786777556767666678889886675475667657787787787767865775767767
7766568767678677678789787567767766578765877788676877777656577
7877887568868788899886865787865777578765576778757988786567778
7867786778678789987776685556876575558666466867875678768865766
8676657566878776986688664657766675468655567776657888866767765
9655756566666866977768787655544475777666786755677768978788888
9677856778565675665866788754554456767665565877677767686588789
9766856776556767775667899785664678876865778888677788665798667
9786667677756888775667877765546558766878887898777886655787778
9788678878656866775558776554467676655786589997686786556876667
9878766866775667567555766566466555766786779787896877657767877
8987885667557877655576576654565677886785566666775787657677867
8996897678665886565656567666766777786885465665786555766665787
9787787876576786577788786865577556876867466565577765788654678
9768996786687775467896885544458577767757767654677776665566568
8867876897886777477898875776547888978676656645787688777667678
7867996877885558897666776677568798658767775677677588878777786
6667776776787667765565677657567676788676687898775577667667778
7557887666876667665566787787786656878556588688878867878547657
8876677566888765767678768868988757777775677777656557789747766
8775665768868756788879867867967577877787876656557757778767665
7666767666567875889979766878977786787775677856557667777876675
If you notice the numbers above, they are not complete random, but slightly offset from each other by no more than 3 in any direction.

What is happening?

We generate an initiate seed,

Code: Select all

set "noise=!random:~-1!"
From this point on, we collect the most recent integer concatenated to the variable NOISE and add 1, 0, -1 to SEED and concatenate to NOISE again..

Once NOISE is created, we create NOISE[] based on the this same principal, except this time we are taking the most recent X position on the CURRENT LINE, and the SAME Y position on the PREVIOUS line, get the average of the 2 + !random:~-1!.

Code: Select all

set /a "seed=(lastXthisLine + lastYlastLine + !random:~-1!) / 3 + 1"
In comparison to a NON-smooth-random method
Image

Code: Select all

2463868193959166302910132570035568339313455820015539645474073
7166682124861612866155277698132819029990059919062506076986339
5073746547450470377149242233225519219163553108010364042728830
2439528397420207403937564665758570816728789423251863278806257
2134235404030429508677947851606191020994717199949054517718523
6529032920575387835785794010992047344411037021077709253502369
2800958281848024010704137767451397848288554385545164648571885
8024787832960600869959205800906627956359316274290223195227007
7690219665131483168751546868182955681004583315880738395930502
7019899534983701227428528832790142004228033920329235849660593
3867969108627505512192687877926969728123496817766528509195016
2751301209918147003428915597744115501017081024987177072302308
4672973791355298830606882497192838215872597000904289872832831
2713356742210425434163225271355294131198659906155266275949513
4758404975924007894435460434764852054846246623709557858539020
4759019261048260360574782895147405223256872172466817522840741
9013636720419252832551106547510966732692821896461174691349790
2304477880765157620269725252816268844555711220013542893218060
8493373632922890889585755488317396158244468665554269203039730
6131817564550894957824673119894130212053984197732716531379109
4326359476313810911860252599913461661790381650384298548113291
0115941729733296011077367615108443619163022916297841289258360
7909710029361263034457874672156914623138959492206558739386921
8101069010099140039373551038776078089698842485266315267410429
0153090478672243378541208502698257173830449588197850970970051
5618996147938885022379922089390440125364565805855844012479489
2272056258099397648632571873253113882127774091136425544657844
3428629277912670801513885135153450423353330867088424914794986
4002135256124684364985793764185898156002506347508433945046526
5166665306762815948637892804479816413312688230334386859284737
3344669953382025899888599151051141415106588105557062936174064
6378341703857331655477491753941556033852348343732294523550047
8621600723464082337680334506844036966738460897558875895624944
4336986969637707117898131804877748780014554212143269978396157
7222960177269703643353358318035632884362550114935538798990027
2087891841847117816360793428374903219827050751253693792865452
4064234793564382841733880335957389279592169651472813707698372
9842055769783436031937464136612187799603090519349467506089220
7567862879747338468372737611655066293916593648060631087223913
8289269581331910636243433801147934164231614197520980668814419
0364955293764331320038938551450138325026285594011967887995300
4374890556842206504436549507984078510516250138451577351677519
4145332748949344516166448488713805371434430352374063405231065
4382520218088054182452424957357535826591294888969255512670008
0631814304373189467898917375765296611603074819849170953425939
4701000948651313942645561596556146972944239355323063815996063
1481761017513441868104387285477199935688887485779092805349532
9146542919374198462064745182813493370598076494165786398666231
3131226949579313181708967902142768006772578939433203109593142

Overall, besides a demonstration, I was wondering if there was a faster way to achieve a similar result? Or may even a more fantastic result than what I have come up with.

Thank you for your time :)

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Smooth Random Numbers

#2 Post by Aacini » 28 Mar 2023 13:49

May I suggest you to test my RdRand.exe small utility to generate much better random numbers?

Antonio

IcarusLives
Posts: 161
Joined: 17 Jan 2016 23:55

Re: Smooth Random Numbers

#3 Post by IcarusLives » 28 Mar 2023 15:53

I'm afraid this isn't exactly what I'm looking for. With my examples posted above, the code I wrote does generate a smoother string of random numbers than "%random% %% 10" alone.

My goals are to have each generated number influenced by it's surroundings much like perlin/simplex noise.

The reason I'm looking for an array of noise[] is for accessibility later on, like in the example above, I map the smooth random iteration to a color value of 0-255, and color each character on the map specific colors but at different amounts of that color. (like darker or brighter greens).

But it would be nice to achieve an effect similar to "light hitting the side of a sphere" so to speak.

I think generating colorful textures this way can be a really interesting way to express art. Any ideas????

Post Reply