< Back to IRCAM Forum

OM nth-random function and code different result?

Hi,

I enclose a patch, I have a different result in using nth-random as function or inside code.
I don’t understand if I am having some mistake, because the result in code 01 is not what I would expect. I have resolved as in code 02, but I am interested to know if I am in error with code 01 or there is something else wrong.

nth-random.omp (5.0 KB)

Thank you in advance for help.

Best

fdsdb

Hmmm…

Very interesting after effect. It seems if you use:

(repeat-n (nth-random '(a b c)) 10)

This has the effect graphically as:
Screenshot_2021-01-22_16-18-46

meaning an eval-once on the nth-random function. It seems that repeat-n has some weird after effects when used graphically. Better use your 02 solution with loop or better use a second argument for the n repeat like so:
Screenshot_2021-01-22_16-21-33

Well anyhow, i will ask the experts around me about this, in order to determine if it is a bug or just a feature…

Thanks a lot for reporting this.

Best
K

Hi Karim,

Thank you for reply and suggestion in code. Yes, I was imagining perhaps it evaluated only once…

I was going crazy trying to understand what was not correct in code.

Ciao

Fabio

Hi Karim,

Sorry to revive this old post, but perhaps the same thing happens with om-random function. I enclose a figure, if you evaluate functions or code you have different results (I hope I was right in writing code, it is very hot in Italy…)

immagine

Ciao

Fabio

Dear Fabio,

No worry. Here the weather is very gentle with us!
This is very interesting. I believe this is because repeat-n is enclosed in the lambda function which in lisp is like this:

(apply #'(lambda (min max times) (repeat-n (om-random min max) times)) '(1 10 10))

in order to have the same behavior than the visual code on the left, you need to use it like so:

Hope this helps.

Best and hope the heat wave will cease shortly!

K

Dear Karim,

Good news for weather, then I should have to come in Paris… :slight_smile:

Thank you, in fact I have resolved by a loop, only I guessed if, as you say some post before, this is a inner feature of random functions code (nth-random and om-random) or it will be resolved/changed in future.

Just for not annoying you with similar questions in future (it could become hotter :slight_smile: )

Many thanks, as usual, ciao

Fabio

Dear Fabio,

Sorry didn’t read the beginning of the thread. Yes, in fact it is not a problem with random function but rather with repeat-n which apparently doesn’t evaluate as expected in its code form. I will see if it is “fixable” ie, without breaking it. For the time being, please do use the loop form instead if you are using it in lisp form.

Best
K

No problems at all, and good to know.

Thank you very much, all my best, ciao

Fabio

There is a solution to bypass the eval process, it is by using a macro:

(defmacro n-repeat (n &rest body)
   `(loop repeat ,n collect (progn ,@body)))

(n-repeat 10 (random 10))

You can then use it either as a lisp expression or a visual lisp call. This will work.
But it will not work if it is called inside a lispfunction patch unfortunately.

Best
K

OK, thank you very much dear Karim, I will test it.

Ciao

Fabio