This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
cw4:4rpl_tools [2025/06/28 18:36] – [Next UseFul Function goes here] kalli | cw4:4rpl_tools [2025/06/30 20:12] (current) – [Pseudo Random Number Generator, based on sinus] kalli | ||
---|---|---|---|
Line 3857: | Line 3857: | ||
- | ===== Pseudo Random Number Generator, | + | ===== Pseudo Random Number Generator, |
<hidden click here for source code> | <hidden click here for source code> | ||
- | A basic function to request a pseudo random number, | + | Several |
- | Input is an integer seed + the desired amount of digits. | + | Copy the functions directly into your script or run the below script in your cpack and have other scripts send messages to request a randfloat or randint. |
- | If you want no truncation, then use 0 for the desired amount of digits. If you request more digits than your seed would generate, then the too short result | + | Difference between |
+ | * " | ||
+ | * " | ||
+ | * " | ||
+ | * No prefix: these are sequenced seeds and they use index 0. | ||
- | < | + | Example code with messages: |
- | 1111 3 @linearPRNG | + | < |
+ | 12345 ->int | ||
+ | 1 -> | ||
+ | 0 -> | ||
+ | 100 ->last # The last integer itself will be excluded. | ||
+ | " | ||
+ | < | ||
+ | " | ||
+ | < | ||
+ | " | ||
+ | < | ||
+ | " | ||
+ | < | ||
+ | " | ||
+ | < | ||
+ | " | ||
+ | < | ||
+ | " | ||
+ | < | ||
+ | " | ||
+ | < | ||
+ | </ | ||
- | :linearPRNG | + | ---- |
- | # INPUT: INTEGER SEED + INTEGER | + | |
- | # OUTPUT: | + | <code SinusRNG.4rpl> |
- | # https://en.wikipedia.org/ | + | :once |
- | dup ->digits | + | # Creating lists and a starting mapconstant |
- | 16807 mul 2 31 pow 1 sub mod2 dup log10 ceil <-digits dup2 gt if sub 10 swap pow div asint else pop pop endif | + | createlist |
- | else | + | createlist -> |
- | 16807 mul 2 31 pow 1 sub mod2 | + | getmapsize 2 div swap 2 div swap dup2 getterrain 1 add dup 99999 floodfillterrain getlistcount -> |
- | endif | + | |
+ | # Setting up the messages so that other scripts can request a random seed. | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | |||
+ | : | ||
+ | <-_DATA listtostack @sinRandInt -> | ||
+ | : | ||
+ | @sinRand01 -> | ||
+ | : | ||
+ | <-_DATA listtostack @indexSinRandInt -> | ||
+ | : | ||
+ | <-_DATA @indexSinRand01 -> | ||
+ | : | ||
+ | <-_DATA listtostack @spikedSinRandInt -> | ||
+ | : | ||
+ | @spikedSinRand01 -> | ||
+ | : | ||
+ | <-_DATA listtostack @seededSinRandInt -> | ||
+ | : | ||
+ | <-_DATA @seededSinRand01 -> | ||
+ | |||
+ | : | ||
+ | ->last | ||
+ | -> | ||
+ | 0 @indexSinRand01 <-last <-first sub mul <-first add asint | ||
+ | |||
+ | :sinRand01 # INPUT: none. | ||
+ | 0 @indexSinRand01 | ||
+ | |||
+ | : | ||
+ | ->last | ||
+ | -> | ||
+ | @indexSinRand01 <-last <-first sub mul <-first add asint | ||
+ | |||
+ | : | ||
+ | # The random numbers will be generated with the previous seed that is stored under that index. | ||
+ | ->i | ||
+ | < | ||
+ | <-i < | ||
+ | 1 -> | ||
+ | else | ||
+ | < | ||
+ | endif | ||
+ | |||
+ | : | ||
+ | ->last | ||
+ | -> | ||
+ | elapsedtime asint @seededSinRand01 <-last <-first sub mul <-first add asint | ||
+ | |||
+ | : | ||
+ | elapsedtime asint @seededSinRand01 | ||
+ | |||
+ | : | ||
+ | ->last | ||
+ | -> | ||
+ | @seededSinRand01 <-last <-first sub mul <-first add asint | ||
+ | |||
+ | : | ||
+ | # abs < | ||
+ | 1.05 pow 99419 sub sin 619 mul dup floor sub | ||
</ | </ | ||