Old 12-16-2017, 05:32 AM   #1
IXix
Human being with feelings
 
Join Date: Jan 2007
Location: mcr:uk
Posts: 3,889
Default JSFX: rand() behaviour strangeness

If you run this, it generates random numbers between n and -n...
Code:
@init
bad = 0;
n = 1;

@sample
v = rand(n*2) - n;
(v > n || v < -n) ? bad = 1; // Outside specified range!
Works fine with any value of n I've tried except values less than 0.5. If you set n to 0.499 or less then v will eventually be assigned a value outside the desired range.

What's that all about then?
IXix is offline   Reply With Quote
Old 12-17-2017, 04:08 AM   #2
IXix
Human being with feelings
 
Join Date: Jan 2007
Location: mcr:uk
Posts: 3,889
Default

Quote:
Originally Posted by Veto View Post
Do you need much precision? I.e. can you work around it by doing...
No, I was just surprised by the result and wondered what was the cause. I couldn't remember if rand() worked with fractions or not so I did a test and wondered how small a value you could use. I might have expected it to go wrong with really tiny values but 0.5 seems a little high. Presumably a bit thing.

There are always workarounds of course but in order to work around something, you have to know it's there in the first place. If this is expected behaviour then perhaps a warning in the docs might be in order?

edit: And of course this is assuming that rand() is the problem but it might not be. I should do a simpler test...

Last edited by IXix; 12-17-2017 at 04:10 AM. Reason: fairness
IXix is offline   Reply With Quote
Old 12-17-2017, 04:12 AM   #3
Tale
Human being with feelings
 
Tale's Avatar
 
Join Date: Jul 2008
Location: The Netherlands
Posts: 3,645
Default

It seems that rand(n) accepts only integers for n, so e.g. 2.0 will give you [0.0..2.0), but so will 2.1, 2.5, and 2.9. I guess n can't be smaller than 1, so 0.x will be the same as 1.

Code:
desc:rand
slider1:1<0,10,0.001>n

@slider

n = slider1;

a = n;
z = 0;

@sample

x = rand(n);
x < a ? a = x;
x > z ? z = x;
Tale is offline   Reply With Quote
Old 12-17-2017, 04:24 AM   #4
IXix
Human being with feelings
 
Join Date: Jan 2007
Location: mcr:uk
Posts: 3,889
Default

Quote:
Originally Posted by Veto View Post
apparently x in rand(x) has to be >= 1.
Ah yes, I see what you mean. Return values for input less than one will overshoot sooner or later...
Code:
@init
bad = 0;
count = 0;
n = 0.9;

@sample
!bad ?
(
  count += 1;
  v = rand(n);
  (v > n) ? bad = 1; // Naughty!
);
...which isn't a problem as long as you know not to use values less than one. A warning in the docs seems appropriate.
IXix is offline   Reply With Quote
Old 12-17-2017, 04:29 AM   #5
IXix
Human being with feelings
 
Join Date: Jan 2007
Location: mcr:uk
Posts: 3,889
Default

Quote:
Originally Posted by Tale View Post
It seems that rand(n) accepts only integers for n, so e.g. 2.0 will give you [0.0..2.0), but so will 2.1, 2.5, and 2.9...
Ah, that makes sense. I guess the input must be cast to int and loses the decimal part.

Last edited by IXix; 12-17-2017 at 04:30 AM. Reason: carappy spellling
IXix is offline   Reply With Quote
Old 12-17-2017, 05:33 AM   #6
mschnell
Human being with feelings
 
mschnell's Avatar
 
Join Date: Jun 2013
Location: Krefeld, Germany
Posts: 14,686
Default

Quote:
Originally Posted by IXix View Post
Ah, that makes sense. I guess the input must be cast to int and loses the decimal part.
IMHO not really. In fact there are two styles of rand() functions. Usually one (called without an argument) provides a floating point random number between 0.0 and 1.0 ("infinite" count of different possible values), and the other (called with an integer argument "n") provides an integer random number in the range of 0 ... n-1 )i.e. n different possible values).

As EEL does not feature integer variables, this is slightly queer here.

-Michael
mschnell is offline   Reply With Quote
Old 12-17-2017, 08:59 AM   #7
IXix
Human being with feelings
 
Join Date: Jan 2007
Location: mcr:uk
Posts: 3,889
Default

Quote:
Originally Posted by mschnell View Post
IMHO not really...

As EEL does not feature integer variables, this is slightly queer here.
You're right, it doesn't actually make sense. I was using "that makes sense" in more of an "I understand" way because I can see why it happens due to the underlying C nature of the EEL engine.

The entry for rand() in the docs ought to mention this little quirk.
IXix is offline   Reply With Quote
Old 12-17-2017, 10:57 AM   #8
ashcat_lt
Human being with feelings
 
Join Date: Dec 2012
Posts: 7,271
Default

Is it much more efficient to run rand(n) rather than rand()*n? Cause it never occurred to me that the result might be anything other than (0...1). In fact, without having read the docs (or seen this thread) I would have assumed that any value I passed in would be used as a seed. So then I wonder - is it faster? Probably, I suppose...
ashcat_lt is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 04:35 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.