< Back to IRCAM Forum

Return integer values randomly with no duplicates

Hello All,

I was wondering if anyone might be able to suggest a solution…

I have a list (0 1 2) from which a single integer should be returned each time the function is called, eg. (0) (1) (2) (1) (0) (2) (0) (1) etc., without a repeated integer. To be clear, this: (0) (1) (2) (1) (0) (2) (0) (1) etc. is good, this: (0) (1) (2) (0) (2) (2) (1) (0) (0) (0) (1) (2) (2) (0) etc. is bad.

I’ve tried nthrandom selecting from (0 1 2), permut-random with first-n, om-random, and others, but all generate a duplicated integer from the list (0 1 2). Is there a way to avoid the duplication?

Thanks very much in advance!!

Hi krhes

Here try this. (put in patches folder in om) Launch om, and call rand-with-no-succ-rep.
The first input is for the list
the second ntimes.

Hope it is this you’re hoping for…

Best
K

no-successive-random.lisp (247 Bytes)

Hi Krhes.

A simple way is to use the new “Patterns” library, where you will find the “p-heap” class, which does exactly what you are asking for.

You’ll find the Patterns library here: http://repmus.ircam.fr/openmusic/libraries. After loading it, import the tutorial patches (“Help”->“Import tutorial patches”->“Patterns”, and most things should be explained.

-anders

heap-pattern.png

Karim’s suggestion above is more precise, ensuring no direct repetition.

I’ve added a new keyword to p-heap - :elide-last? - which if set to t will take care to return new periods which can’t start with the last sample from the previous period. This should leave no direct repetition anywhere in the result.

p-heap-with-elide-last-keyword.png

Thanks very much indeed, Anders and Karim! I’ll try your solutions now.

Good wishes,
krhes.

Thanks very much indeed, Anders and Karim! I’ll try your solutions now.

Forgive the follow-up question but I’m sure this is simple (just not for me it would seem!). Having evaluated a lisp code in the lisp editor and successfully used it in om, where do I save the lisp buffer to be able to use the function in a new om session? I’m currently saving the code as a comment file in a patch and then re-evaluating it in order to revive the function, which can’t be the most efficient way of doing things…

Good wishes, and thanks again,
krhes.

Hi Anders,

I downloaded the library, inserted the file into the library folder and tried to open the library within om. This message appeared:

Error while loading the library Patterns_0.98:
The init file “/Applications/OM 6.12/libraries/Patterns_0.98/Patterns_0.98.lisp” does not exist.

I looks likes everything is there but after two attempts I still get the same error message. Sorry to ask for further guidance, but any thoughts on why this is happening?

Thanks in advance,
krhes

Hi Karim,

Thanks very much for your patch which is very helpful! However, I didn’t make myself 100% clear, for which apologies.

Rather than a list (0 1 0 2 0 1 0 2 1 0 1), I’d like a single integer 0, 1 or 2 that doesn’t duplicate on successive calls.

So, I’d like to see something like the following in the om listener:

OM => (0)
OM => (1)
OM => (2)
OM => (1)
OM => (2)
OM => (0)
OM => (1)

etc.

Is there a way to adapt your patch with another function or does it need a different approach?

Thanks so much in advance,
krhes

Dear Krhes,

Concerning the lisp code, i am wondering, does this concern the code i have sent you ? If yes well here all the answers:

  1. Put your code in the patcehs folder you will find in OM’s folder first.
  2. Then , LAunch OM. that way the code will be loaded automatically. Every lisp code you will put in the patches folder will be laoded automatically at the start of the application, being really carefull that in each header’s file there is the following expression : (in-package :om)
  3. Once these code are loaded, you can open any patch , cmd+click (Macos) or ctrl+click (linux/win) and enter the name of the function then click elsewhere… You will have the graphical veersion of the lisp code. Then you will be able to use it as any Lisp function (or method).

Hope this helps…
PS: I am attaching it in a patch.

BEst
K

rand-no-successives.omp (1.1 KB)

Hi Karim,

Thanks very much for this! Your patch worked perfectly as sent. I was referring not to your lisp, but to some adapted lisp code I used.

Your advice solved my problem however! Thank you!

Finally, if you also have any thoughts on how to achieve

OM => (0)
OM => (1)
OM => (2)
OM => (1)
OM => (2)
OM => (0)
OM => (1)
etc.
instead of a list (0 1 2 1 2 0 1) in the om listener whenever you have time I would be very grateful.

Again, thank you!
krhes

Dear Khres,

It is possible to have a list of list using (mapcar 'list ). But i don’t beleive that this is what you want to do.
YOu want this returned in the listener one by one in MAX style. Well, i don’t know the reason why you want to do such a thing, but if you want to do so , i suggest using MAX for direct output. This is not the CommonLIsp interpreted style. It becomes somehow complicated to do so with OpenMusic. Maybe Jean can help you out. He’s been working on re-active programing maybe this could help.

Best
K

hello,

karim is right regarding the lisp style. it seems you need a non-immediately-repeated result in after each eval, is that? in that case it might be some way of faking it. perhaps you can define a list with the succesive results you need and store it. then you can retrieve each sublist with nth function if you can store an index AND update it after each eval. setf would be handy i guess. but this would probably be a solution available to use within a patch only.
i hope i made clear myself, i’m not a programmer nor a native english speaker.
regards,
perti

hi

try this (made with OM 6.11)

Carlos

Patch.omp (3.77 KB)

Hi,

I can’t thank everyone enough for their suggestions - which are illuminating about how to formalise a problem as well as helpful.

Carlos - thank you very for your patch. This does EXACTLY what I hoped for.

Very best wishes to the you all,
krhes

I downloaded the library, inserted the file into the library folder and tried to open the library within om. This message appeared:

Error while loading the library Patterns_0.98:
The init file “/Applications/OM 6.12/libraries/Patterns_0.98/Patterns_0.98.lisp” does not exist.

Hmm… not sure what happens, seems to be something with the naming. When unpacking the library here i don’t see any version number (_0.98). Make sure there’s a folder named “Patterns” and a file inside there named “Patterns.lisp”.

So, I’d like to see something like the following in the om listener:

OM => (0)
OM => (1)
OM => (2)
OM => (1)
OM => (2)
OM => (0)
OM => (1)

Using the Patterns lib in the attached png. (make sure to use the latest version (0.99) because of the :elide-last? keyword.)

heap-pattern-v-2.png