0

i'm trying to make a seeded random number generator. all i want as a function to take in a seed, increment and max value and then return a result. i'm not too worried about things being uniform, but the more uniform the better. the purpose for this is to generate a random sequence of notes for music.

so far from searching, it seems everyone includes some sort of library, which i don't have the luxury of. I am using REAPER's JSFX language which is based off cockos EEL2 which is based off c. there is a rand() function but there is no way to seed it. i've tried creating my own methods such as (seed * increment) % maxvalue however if the max value is at 10, the order of random numbers is always ascending.

i've also tried searching online, even resorting to chatgpt. but everything i've seen doesn't make sense.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • 1
    A simplest random number generator would be `int rng(void) { return 5;}` as in https://xkcd.com/221/ . Jokes aside - what is the problem exactly? It is just a function that will probably have a static state and will return a result of *some* calculation based on this state. The rest is details. – Eugene Sh. May 18 '23 at 15:18
  • 1
    Some formulas and specific parameters for inspiration: https://en.wikipedia.org/wiki/Linear_congruential_generator – teapot418 May 18 '23 at 15:19
  • it has `rand()` but no [`srand()`](https://linux.die.net/man/3/srand)? – yano May 18 '23 at 15:20
  • no srand that i know of https://www.cockos.com/EEL2/index.php#userfunc – kurpingspace2 May 18 '23 at 15:21
  • Does the generator have to be cryptographically secure? Or are you just looking for a way to generate seemingly random numbers in a repeatable way? – msaw328 May 18 '23 at 15:21
  • So `rand(5)` for instance always returns the same number between 0 and 5? Documentation says it won't, maybe I'm not understanding what you want here. – yano May 18 '23 at 15:25
  • 2
    `splitmix64?` [Here](https://xorshift.di.unimi.it/splitmix64.c) is a version that uses a local static. You could just make that a parameter and call that the seed. – Jason May 18 '23 at 15:25
  • I am creating a midi arpeggiator plugin, and i want to have a random note order. But i would also like to use this same random number generator to generate other random numbers for things such as panning, note order, note velocity, My note index is variable from 0 to any amount of notes being played by the keyboard. note velocity goes 0 to 127. it would be nice to have everything on its own seed so i can fine tune how a song can sound, for example you like the note order, so you keep it. or you don't like the velocity, so you change that seed. – kurpingspace2 May 18 '23 at 15:26
  • That sounds like you've learned from the mistake made when creating THX Deep Note. – Agent_L May 18 '23 at 15:28
  • oh also, the increment value is so i can keep playing the same order of random notes by a seed. so when i start from time 0, it will increase when i press play. every 1/4th bar. or every "second" if the tempo is 120. Basically this value increases as time goes on, and when you want to go back to the start, the time goes back to 0. it is a float variable as well. every 1/4th beat is a whole number. so 1 bar would be the number 4. 16th would be 0.25 – kurpingspace2 May 18 '23 at 15:30
  • Sounds like we have some XY-question here. – Eugene Sh. May 18 '23 at 15:33
  • 1
    If you want to preserve random sequences, can't you create them and store them for later use? – ad absurdum May 18 '23 at 15:33
  • i've been thinking about storing a random sequence, but i'm not sure. after closing and reopening my project, how do i preserve these randomly generated lists? i guess by using a file but i'm not really sure... not really the solution i'm looking for... plus also there's the issue of also playing back notes in an asynchronous environment. when the project is paused, you still want to hear what you play for ideas and such. pressing play just enables a laboratory setting where you can expect the same notes to appear. – kurpingspace2 May 18 '23 at 15:38
  • also i don't know how long a song could be so generating a sequence of random numbers would need to be very long, could consume unwanted ram and file space. i don't know. i just don't like the idea. – kurpingspace2 May 18 '23 at 15:40
  • to index memory on JSFX you add the SUM of two numbers to access memory. so 0[0] is memory 0. 1000[0] is the 1000th memory slot. depending on how frequent notes play, it could sound like its looping if i don't give it enough notes to play with. – kurpingspace2 May 18 '23 at 15:42
  • I don't get why you need a different RNG for each aspect of tone generation. If the want a velocity range of 0 to 127 then it's `rand() % 128`, and if you want to tune random behaviour, it's not the generator itself that needs attention, but the way you use its results: varying within particular constraints. – Weather Vane May 18 '23 at 15:50
  • also, assuming you mean to use the rand() function to generate this list, which could work. i could not seed it. if i wanted to change how the notes play, but i want to undo the change in the seed, i cannot. having the seed allows for more control – kurpingspace2 May 18 '23 at 15:51
  • ill try some of these solutions later, need to make sense of them – kurpingspace2 May 18 '23 at 15:54
  • `int myrand( void ) { return 4; } # Picked by random dice roll` (Credits to xkcd) – ikegami May 18 '23 at 16:18
  • 'n' problems, 1 solution. Use 'mersenne twister' – hsuecu May 18 '23 at 16:45
  • ok... so, i've tried to implement all the code here and some other things. https://stackoverflow.com/questions/52514296/simplest-random-number-generator-without-c-library i tried this one, however it does not give all numbers from 0 to 10, it gave me 2, 4, 6 and 8. i've tried a few solutions where you use xorshifts but for some reason most of the time i just get a result of "1" maybe something to do with how eel2 handles the numbers or something . i am not sure. the only things i haven't tried is the linear generator and the twist since that requires me to use my brain – kurpingspace2 May 19 '23 at 15:51
  • i've implemented the use of a LCG. i'll post an answer to close this discussion soon, once i fully implement it and confirm there are no issues. – kurpingspace2 May 19 '23 at 20:28

0 Answers0