Group Admins

  • Profile picture of Karim
  • Profile picture of Jean

OpenMusic

Public Group active 22 hours, 39 minutes ago

User group for OpenMusic and computer-aided composition. Visit the Forum for discussions.

return integer values randomly with no duplicates

Author 5 Subscribed Users |
Profile photo of krhes
krhes

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!!

July 31, 2017 at 17:28 #23293
Profile photo of Karim
Karim

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

July 31, 2017 at 17:49 #23294
Profile photo of anders
anders

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

Attachments:
  1. heap-pattern

    heap-pattern.png

  2. heap-pattern

    heap-pattern.png

July 31, 2017 at 17:55 #23297
Profile photo of anders
anders

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.

Attachments:
  1. p-heap-with-elide-last-keyword

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

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

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

July 31, 2017 at 20:08 #23300
Profile photo of krhes
krhes

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

Good wishes,
krhes.

July 31, 2017 at 20:36 #23303
Profile photo of krhes
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.

July 31, 2017 at 20:42 #23304
Profile photo of krhes
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

July 31, 2017 at 21:07 #23305
Profile photo of krhes
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

July 31, 2017 at 21:26 #23306
Profile photo of Karim
Karim

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

July 31, 2017 at 21:29 #23307
Profile photo of krhes
krhes

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

July 31, 2017 at 21:51 #23310
Profile photo of Karim
Karim

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

July 31, 2017 at 23:27 #23311
Profile photo of perti
perti

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

August 1, 2017 at 00:13 #23312
Profile photo of agonc
agonc

hi

try this (made with OM 6.11)

Carlos

Attachments:
  1. Patch.omp
  2. Patch.omp
August 1, 2017 at 01:27 #23313
Profile photo of krhes
krhes

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

August 1, 2017 at 06:13 #23316
Profile photo of anders
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.

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”.

August 1, 2017 at 12:10 #23317
Profile photo of anders
anders

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.)

Attachments:
  1. heap-pattern-v-2

    heap-pattern-v-2.png

  2. heap-pattern-v-2

    heap-pattern-v-2.png

August 1, 2017 at 12:15 #23318

You must be logged in to reply to this topic.

Log in now