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:29] – kalli | cw4:4rpl_tools [2025/06/30 20:12] (current) – [Pseudo Random Number Generator, based on sinus] kalli | ||
---|---|---|---|
Line 3857: | Line 3857: | ||
- | ===== Next UseFul Function goes here ===== | + | ===== Pseudo Random Number Generator, based on sinus ===== |
<hidden click here for source code> | <hidden click here for source code> | ||
- | <code 4rpl file name.4rpl> | + | Several basic functions based on Grabz' rng lite function of "<-seed sinus 10000 mul dup floor sub". |
+ | |||
+ | 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. | ||
+ | |||
+ | |||
+ | Difference between the functions: | ||
+ | * " | ||
+ | * " | ||
+ | * " | ||
+ | * No prefix: these are sequenced seeds and they use index 0. | ||
+ | |||
+ | Example code with messages: | ||
+ | <code example.4rpl> | ||
+ | 12345 ->int | ||
+ | 1 ->index | ||
+ | 0 ->first | ||
+ | 100 ->last # The last integer itself will be excluded. | ||
+ | " | ||
+ | < | ||
+ | " | ||
+ | < | ||
+ | " | ||
+ | < | ||
+ | " | ||
+ | < | ||
+ | " | ||
+ | < | ||
+ | " | ||
+ | < | ||
+ | " | ||
+ | < | ||
+ | " | ||
+ | < | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | <code SinusRNG.4rpl> | ||
+ | :once | ||
+ | # Creating lists and a starting mapconstant for the seed sequences. | ||
+ | createlist -> | ||
+ | createlist -> | ||
+ | getmapsize 2 div swap 2 div swap dup2 getterrain 1 add dup 99999 floodfillterrain getlistcount -> | ||
+ | |||
+ | # 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 -> | ||
+ | |||
+ | :sinRandInt # INPUT: integer first randInt + integer last randInt. OUTPUT: an integer in between the first and last randInt, excluding the last randInt. | ||
+ | ->last | ||
+ | ->first | ||
+ | 0 @indexSinRand01 <-last <-first sub mul <-first add asint | ||
+ | |||
+ | :sinRand01 # INPUT: none. | ||
+ | 0 @indexSinRand01 | ||
+ | |||
+ | : | ||
+ | ->last | ||
+ | ->first | ||
+ | @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 | ||
+ | ->first | ||
+ | elapsedtime asint @seededSinRand01 <-last <-first sub mul <-first add asint | ||
+ | |||
+ | : | ||
+ | elapsedtime asint @seededSinRand01 | ||
+ | |||
+ | : | ||
+ | ->last | ||
+ | ->first | ||
+ | @seededSinRand01 <-last <-first sub mul <-first add asint | ||
+ | |||
+ | : | ||
+ | # abs <-power pow <-add add sin <-sinMul mul dup floor sub | ||
+ | 1.05 pow 99419 sub sin 619 mul dup floor sub | ||
</ | </ | ||