Skip to content

Instantly share code, notes, and snippets.

@andrew-raphael-lukasik
Last active September 28, 2023 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrew-raphael-lukasik/b767c82969eb67ae76b6351bd31bb558 to your computer and use it in GitHub Desktop.
Save andrew-raphael-lukasik/b767c82969eb67ae76b6351bd31bb558 to your computer and use it in GitHub Desktop.
lets run some test and figure out how to generate useful random numbers in a `Unity.Collections.IJobParallelFor` run

Test to figure out how to generate useful random numbers in a Unity.Collections.IJobParallelFor

seed: 46473452

(reference) new Random( Seed * (uint)( index + 1 ) ).NextInt() new Random( Seed + (uint)( index * Results.Length ) ).NextInt() Random.CreateFromIndex( Seed ^ (uint)index ).NextInt() new Random( Seed * Random.CreateFromIndex( (uint)index ).NextUInt() ).NextInt() new Random( Seed * Random.CreateFromIndex( (uint)index ).state ).NextInt() new Random( new Random( Seed * Random.CreateFromIndex( (uint)index ).NextUInt() ).NextUInt() ).NextInt()
-37321856 -37321856 -37321856 501644091 673738796 2057345775 -477813603
-1687388613 2073921280 -97452083 -1206518005 327600797 -2026729932 -1871146698
-2118002258 -421164753 -83806521 -530221473 -1985565071 1850284329 -781137724
-462119340 2000358945 -128750088 -2079259769 -2097652973 -913901041 -1160125377
-2139958037 -1878180071 -124933267 461841453 461778925 1850233261 285973233
-2028164580 1306235519 -103816742 28705499 -1253204410 -28296093 -701833753
1504904531 436692450 -163030469 -1612281161 1065612782 1833310253 1727568690
440271423 1852218434 -150335348 -1942934782 -1728908812 -354186381 526049333
-1790955677 -518057633 -137046503 577417521 -803717934 458249623 667211024
-2061196907 -1608876494 -192508634 940740762 343901289 -1989785234 2057598001
870279902 -190802694 -172538836 1044741980 -1315705096 -445217503 1172845297
236205596 463971551 -230606751 1112605289 -2080689883 386906028 1580202667
-1843266409 -555779326 -216796590 597357448 -84251000 -2123377431 1383610194
466764686 -1274098715 -203574205 -2125792439 1642170959 -1394693055 1168925018
668901823 -1292841425 -255955216 -1277798897 583145778 365954866 1567453603
-1899597525 1556953253 -243785691 428576014 1756117585 624213495 321370563
1552767514 381391229 -287698194 214002373 1772949269 731604205 -1044722349
235544157 1110352543 -300657409 -1497716891 523813994 1563584506 -251571757
-168112355 -928094900 -282747403 -751776894 940571265 1094957143 749010127
792091441 -1070269340 -327694646 139865268 1435389484 -1040301945 355410382
1851353657 1145223932 -308213605 894436882 703827432 -503884135 1370438080
551247222 1765878260 -355241804 271643232 -2073888992 378600648 -1968803393
634052940 594903581 -366168279 -871576720 498245724 -473260045 2123183207
1166060061 -1218459233 -345047650 -1760967409 -1982251167 -1724257936 1754277690
1219440124 -1215083172 -391634361 -442045218 -1045358120 -1757776112 -1275929902
252171764 1035925029 -370513680 759061355 -4983182 -1036815046 -1362686161
1117030952 1057602543 -424946366 -385869509 -777939323 -1814759604 -1923759940
-1444553792 -401795126 -429550765 -1254814443 1466435532 -2127385788 -1515156381
1093649230 -1486324067 -415803040 128491640 -2035277019 -972726289 -666834034
-1853496755 -437183394 -455014851 -1574070371 1705984518 -389461926 806582962
-1806794293 1137381362 -443364090 -1466634246 -108605083 1873729766 1458181777
-897057296 966422858 -446919913 -739782707 -1259577931 -824909254 -737492679
-1844622612 1748273150 -499223620 -1868288238 1996703036 634793469 -349620279
1363312580 -1385716998 -470270615 -1194616571 443818444 774765652 1474597709
-907238471 -1885728999 -522590741 -2082839642 1610603257 -1438948713 817487272
-286899212 72205599 -509890724 1693440005 23297306 127876289 -1228577447
-175470664 -1711567517 -515544631 917971990 1467186683 426273309 557863452
181351878 291293881 -537476346 -589977973 745832675 -1350939257 829621270
1653587037 1654656047 -570356641 1435118226 699429675 -2007211656 -579241792
898026815 7960776 -581740704 1192706361 -2077222380 -393139756 1765625066
1744993999 -1672416647 -577924619 -1442844197 1072972381 -1116470898 1078258147
-593335966 142964216 -590357694 958976475 -223750356 1049720213 -1042353491
-1605757691 -1696137648 -607248176 862561300 -636706224 1578758065 -2032039731
-1078326672 1384272841 -636981755 1273866077 2041551368 -105834753 -687828624
544379978 -1800770376 -649415234 1132771621 -177303216 1942392979 -811368596
764283863 -956595173 -645598289 20252913 -414577573 1248599158 2129311866
1919903757 1210090969 -654886764 1041938738 55233480 -572579031 477797662
1924542714 -289434849 -683605255 2095606782 234856207 -1738354070 1037197345
562315118 -1379271101 -703346486 -289274294 -447411778 213189154 -935609844
1800693530 -282682727 -690123045 -915002755 -1447414158 388014230 32512171

seed: 1

(reference) new Random( Seed * (uint)( index + 1 ) ).NextInt() new Random( Seed + (uint)( index * Results.Length ) ).NextInt() Random.CreateFromIndex( Seed ^ (uint)index ).NextInt() new Random( Seed * Random.CreateFromIndex( (uint)index ).NextUInt() ).NextInt() new Random( Seed * Random.CreateFromIndex( (uint)index ).state ).NextInt() new Random( new Random( Seed * Random.CreateFromIndex( (uint)index ).NextUInt() ).NextUInt() ).NextInt()
-2147213279 -2147213279 -2147213279 164937879 71757027 75173132 -494239249
-2079848959 -2146942910 -2134219216 -797980616 -1459157291 -1459411212 1919001978
499951813 -2146672541 -2120700925 -1222987959 1076552558 1076806479 -1801018226
-1839883953 -2146402172 -2108755106 -1809642375 -1132439535 -1132169168 1204435441
251205585 -2146131803 -2093139867 872426940 422683537 422937520 -2059578607
-1401988144 -2145861434 -2081194380 2127588377 123058528 122247559 -1078563488
-1515048166 -2145591065 -2070837537 2139802918 -1373943832 -1373673527 437720596
-1711727438 -2145320696 -2057844726 -1768629147 -471205774 -471492591 1075394114
-142118619 -2145050327 -2039082872 1917129040 594158884 594412805 -1643581884
768615284 -2144779958 -2026612161 -1430641007 -570952103 -578284153 1151885062
509608651 -2144509589 -2014667606 301691447 1477370146 1476559169 24290740
-652437705 -2144239220 -1999068507 1829064466 -2012974872 -2013245239 -989679989
884493194 -2143968851 -1993380356 1139773803 -455393693 -455647744 -503807287
-2065434450 -2143698482 -1971505469 2034280482 -1986266901 -1986012982 643233417
-2060013579 -2143428113 -1968967594 1251492986 -335977170 -336263859 750794467
1237620145 -2143157711 -1948140831 1526251126 16231218 16501585 1202163417
-1256089336 -2142887408 -1931460237 -1797064229 2076566565 2076296196 795457284
1175706376 -2142616973 -1926885466 -2100206639 -1326834954 -1326564649 -2061179185
-1826475119 -2142346670 -1905994723 -567692517 -207285564 -205917661 1851678109
2136415769 -2142076235 -1901424116 851469684 -518905633 -518651650 70760730
236075571 -2141805932 -1881581257 256595507 -481202533 -480948550 516108987
1674833337 -2141535497 -1863323878 -1740119065 -735970700 -735716779 -2036705468
-755706901 -2141265194 -1849875159 -1588550419 -466403946 -466149961 -206432523
1504911951 -2140994759 -1854708936 1001649619 208338762 208592747 603285384
-2102850830 -2140724456 -1840071750 1059685272 123983633 127414445 -843803696
-585352663 -2140454021 -1827606259 1597382496 -465591430 -469613931 441033073
293050277 -2140183718 -1795813948 1014258204 -319419680 -319673663 -1743110871
-1813787932 -2139913283 -1783347341 128333399 1372787072 1372582247 1281996961
-1118795557 -2139642980 -1790213906 383391965 415039662 414785679 -1379982375
624173374 -2139372545 -1776690223 1390410683 -302008900 -302279267 1918606335
-2089957545 -2139102242 -1748036224 1648445507 -1975971969 -1975701666 -787642315
1265117204 -2138831774 -1728238705 -1836077269 577032486 576778503 -1337078363
-2093798655 -2139085757 -1714625915 1032900540 1840214596 1839403559 -16423504
741621032 -2138291168 -1727880044 -21204302 -1466413944 -1467700113 -2145436573
-642501408 -2138545151 -1707049169 350246405 -1965669095 -1965923016 215064674
-1172097548 -2137750298 -1685704198 -2133834189 -634143852 -633889867 91777875
1328475386 -2138004281 -1664759719 685103709 -884892885 -884622582 -55050184
-997912094 -2137209692 -1676964280 1655796823 -531357197 -531627566 -383227097
-1315080834 -2137463675 -1655094149 -815818426 -400616339 -400870324 1489565061
-110254585 -2136668822 -1634788570 820481385 -1298429762 -1298667299 -1307761125
-133612815 -2136922805 -1614884308 -1982740407 -1768041181 -1768295166 -1425955586
900832794 -2136128216 -1592952605 -474514645 -1413294940 -1413024635 -611906296
-937346749 -2136382199 -1579958666 1067171421 -1388600502 -1388854487 -217623584
237286951 -2135587346 -1601047359 1155172586 1113265396 1113535701 945927857
-2064969545 -2135841329 -1552537072 695801999 -562002495 -561732128 153942364
960871145 -2135046740 -1573625689 1246548443 1199254266 1199508187 1722754699
1292575045 -2135300723 -1561680334 428996770 -1536995381 -1535135956 2128546279
223965012 -2134505901 -1516708323 -978096484 -164300925 -164063264 1885126624
918838957 -2134759822 -1532422377 218330034 617892812 617638893 403349181
523101317 -2133965295 -1484830646 -252119705 231826283 232588040 372376884
// src* https://gist.github.com/andrew-raphael-lukasik/b767c82969eb67ae76b6351bd31bb558
using UnityEngine;
using Unity.Jobs;
using Unity.Collections;
using Random = Unity.Mathematics.Random;
public class RandomnessAndIJobParallelFor : MonoBehaviour
{
[SerializeField][Min(1)] uint _seed = 1234;
[SerializeField][Min(1)] int _numResults = 10;
void OnEnable ()
{
var singleJob = new SingleRandomnessJob{
Seed = _seed ,
Results = new NativeArray<int>( _numResults , Allocator.TempJob )
};
singleJob
.Schedule()
.Complete();
var parallelJob1 = new ParallelizedRandomnessJob1{
Seed=_seed ,
Results=new NativeArray<int>( _numResults , Allocator.TempJob )
};
parallelJob1
.Schedule( parallelJob1.Results.Length , 128 )
.Complete();
var parallelJob2 = new ParallelizedRandomnessJob2{
Seed=_seed ,
Results=new NativeArray<int>( _numResults , Allocator.TempJob )
};
parallelJob2
.Schedule( parallelJob2.Results.Length , 128 )
.Complete();
var parallelJob3 = new ParallelizedRandomnessJob3{
Seed=_seed ,
Results=new NativeArray<int>( _numResults , Allocator.TempJob )
};
parallelJob3
.Schedule( parallelJob3.Results.Length , 128 )
.Complete();
var parallelJob4 = new ParallelizedRandomnessJob4{
Seed=_seed ,
Results=new NativeArray<int>( _numResults , Allocator.TempJob )
};
parallelJob4
.Schedule( parallelJob4.Results.Length , 128 )
.Complete();
var parallelJob5 = new ParallelizedRandomnessJob5{
Seed=_seed ,
Results=new NativeArray<int>( _numResults , Allocator.TempJob )
};
parallelJob5
.Schedule( parallelJob5.Results.Length , 128 )
.Complete();
var parallelJob6 = new ParallelizedRandomnessJob6{
Seed=_seed ,
Results=new NativeArray<int>( _numResults , Allocator.TempJob )
};
parallelJob6
.Schedule( parallelJob6.Results.Length , 128 )
.Complete();
// lets the compare results:
{
var message = new System.Text.StringBuilder($"seed: {_seed}");
message.Append( $"\n|<b>{SingleRandomnessJob.label}</b>|<b>{ParallelizedRandomnessJob1.label}</b>|<b>{ParallelizedRandomnessJob2.label}</b>|<b>{ParallelizedRandomnessJob3.label}</b>|<b>{ParallelizedRandomnessJob4.label}</b>|<b>{ParallelizedRandomnessJob5.label}</b>|<b>{ParallelizedRandomnessJob6.label}</b>|" );
message.Append( "\n|---|---|---|---|---|---|---|" );
for( int i=0 ; i<_numResults ; i++ )
message.Append( $"\n|\t{singleJob.Results[i]}\t|\t{parallelJob1.Results[i]}\t|\t{parallelJob2.Results[i]}\t|\t{parallelJob3.Results[i]}\t|\t{parallelJob4.Results[i]}\t|\t{parallelJob5.Results[i]}\t|\t{parallelJob6.Results[i]}\t|");
Debug.Log( message.ToString() );
}
singleJob.Results.Dispose();
parallelJob1.Results.Dispose();
parallelJob2.Results.Dispose();
parallelJob3.Results.Dispose();
parallelJob4.Results.Dispose();
parallelJob5.Results.Dispose();
parallelJob6.Results.Dispose();
}
}
[Unity.Burst.BurstCompile]
struct SingleRandomnessJob : IJob
{
public static readonly string label = "<sub> (reference) </sub>";
public uint Seed;
public NativeArray<int> Results;
void IJob.Execute()
{
var rnd = new Random(Seed);
int len = Results.Length;
for( int i=0 ; i<len ; i++ )
Results[i] = rnd.NextInt();
}
}
[Unity.Burst.BurstCompile]
struct ParallelizedRandomnessJob1 : IJobParallelFor
{
public static readonly string label = "<sub> new Random( Seed * (uint)( index + 1 ) ).NextInt() </sub>";
public uint Seed;
public NativeArray<int> Results;
void IJobParallelFor.Execute ( int index )
=> Results[index] = new Random( Seed * (uint)( index + 1 ) ).NextInt();
}
[Unity.Burst.BurstCompile]
struct ParallelizedRandomnessJob2 : IJobParallelFor
{
public static readonly string label = "<sub> new Random( Seed + (uint)( index * Results.Length ) ).NextInt() </sub>";
public uint Seed;
public NativeArray<int> Results;
void IJobParallelFor.Execute ( int index )
=> Results[index] = new Random( Seed + (uint)( index * Results.Length ) ).NextInt();
}
[Unity.Burst.BurstCompile]
struct ParallelizedRandomnessJob3 : IJobParallelFor
{
public static readonly string label = "<sub> Random.CreateFromIndex( Seed ^ (uint)index ).NextInt() </sub>";
public uint Seed;
public NativeArray<int> Results;
void IJobParallelFor.Execute ( int index )
=> Results[index] = Random.CreateFromIndex( Seed ^ (uint)index ).NextInt();
}
[Unity.Burst.BurstCompile]
struct ParallelizedRandomnessJob4 : IJobParallelFor
{
public static readonly string label = "<sub> new Random( Seed * Random.CreateFromIndex( (uint)index ).NextUInt() ).NextInt() </sub>";
public uint Seed;
public NativeArray<int> Results;
void IJobParallelFor.Execute ( int index )
=> Results[index] = new Random( Seed * Random.CreateFromIndex( (uint)index ).NextUInt() ).NextInt();
}
[Unity.Burst.BurstCompile]
struct ParallelizedRandomnessJob5 : IJobParallelFor
{
public static readonly string label = "<sub> new Random( Seed * Random.CreateFromIndex( (uint)index ).state ).NextInt() </sub>";
public uint Seed;
public NativeArray<int> Results;
void IJobParallelFor.Execute ( int index )
=> Results[index] = new Random( Seed * Random.CreateFromIndex( (uint)index ).state ).NextInt();
}
[Unity.Burst.BurstCompile]
struct ParallelizedRandomnessJob6 : IJobParallelFor
{
public static readonly string label = "<sub> new Random( new Random( Seed * Random.CreateFromIndex( (uint)index ).NextUInt() ).NextUInt() ).NextInt() </sub>";
public uint Seed;
public NativeArray<int> Results;
void IJobParallelFor.Execute ( int index )
=> Results[index] = new Random( new Random( Seed * Random.CreateFromIndex( (uint)index ).NextUInt() ).NextUInt() ).NextInt();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment