Active TopicsActive Topics  Display List of Forum MembersMemberlist  Search The ForumSearch  HelpHelp
  RegisterRegister  LoginLogin
PowerHome Programming
 PowerHome Messageboard : PowerHome Programming
Subject Topic: rand() function Post ReplyPost New Topic
Author
Message << Prev Topic | Next Topic >>
gg102
Senior Member
Senior Member


Joined: January 29 2013
Location: United States
Online Status: Offline
Posts: 245
Posted: March 06 2014 at 19:56 | IP Logged Quote gg102

I have a "returned home" macro that gets called when I come home. It does a TTS (Text To Speech) function to welcome me home. (My home is so courteous...) In this macro, I use rand() to randomly select a TTS message in a case statement. It all works nicely.

What I am experiencing is that if I call this function repeatedly( by hand), I do in fact get a different random message, but over time, when I do not call it quickly, I seem to always get the first message not a random message.

The way I understand this function is that if I call rand(10) I should get a random number between 1 and 10. What I'm experiencing is mostly 1, and rarely a different number if left over time. (say over 10 or 12 hours)

I'm wondering if this rand() function resets or gets "stuck" over time, or something else is going on. I understand that no random function is truly random and that's not my issue, but this doesn't seem to be at all random if left over time.

ie:
case (rand(5)
WHEN 1 ....message 1
WHEN 2 ....message 2
etc ...)

For now, I might just take mod 10 of the minute of when I return to get a different number.

Has anyone else had a similar finding?



Edited by gg102 - March 06 2014 at 20:41
Back to Top View gg102's Profile Search for other posts by gg102
 
smarty
Super User
Super User
Avatar

Joined: May 21 2006
Location: United States
Online Status: Offline
Posts: 728
Posted: March 06 2014 at 21:19 | IP Logged Quote smarty

GG,
   I too have have seen this same sort of "less than random" behavior
that occurs over time (you describe it well).

I have never tried to work around this, but it bet it could be done.
Maybe exercise the function a couple of times before using the
resultant output....

Edited by smarty - March 06 2014 at 21:20


__________________
Elk - Insteon - BlueIris - DMC1 - PowerHome - XLobby - HA_Bridge w/Dots - Brultech
Back to Top View smarty's Profile Search for other posts by smarty
 
gg102
Senior Member
Senior Member


Joined: January 29 2013
Location: United States
Online Status: Offline
Posts: 245
Posted: March 06 2014 at 21:32 | IP Logged Quote gg102

Smarty,

There's a thought I didn't try. I'll try it tomorrow. Might be interesting.

Thanks for the thought.

Back to Top View gg102's Profile Search for other posts by gg102
 
gg102
Senior Member
Senior Member


Joined: January 29 2013
Location: United States
Online Status: Offline
Posts: 245
Posted: March 07 2014 at 13:59 | IP Logged Quote gg102

Well, today's test had very interesting results.

I made a macro with two lines:
1. set global "var2" rand(10)
2. ph_writefile("c:\random.txt",0, today() + " DEBUG MESSAGE: Random number: {VAR2}, and more are: " +
rand(10)+ ", "+rand(10)+ ", "+rand(10)+", "+rand(10)+
", "+rand(10)+ "." +char(13) + char(10) )

Then I set a TE for 10 minutes to call it, then after an hour, I changed the TE to 31 minutes.

Here's the results:

TE set to 10 minute interval
3/7/2014 09:05:40 DEBUG MESSAGE: Random number: 2, and more are: 9, 10, 2, 4, 4.
3/7/2014 09:15:40 DEBUG MESSAGE: Random number: 4, and more are: 6, 5, 9, 8, 6.
3/7/2014 09:25:40 DEBUG MESSAGE: Random number: 5, and more are: 6, 3, 9, 8, 2.
3/7/2014 09:35:40 DEBUG MESSAGE: Random number: 9, and more are: 10, 9, 3, 4, 5.
3/7/2014 09:45:40 DEBUG MESSAGE: Random number: 7, and more are: 8, 8, 4, 1, 5.
3/7/2014 09:55:40 DEBUG MESSAGE: Random number: 8, and more are: 7, 4, 1, 9, 8.
TE changed to 31 minutes
3/7/2014 10:05:40 DEBUG MESSAGE: Random number: 1, and more are: 5, 1, 4, 8, 4.
3/7/2014 10:36:40 DEBUG MESSAGE: Random number: 1, and more are: 6, 2, 9, 6, 5.
3/7/2014 11:07:41 DEBUG MESSAGE: Random number: 4, and more are: 9, 9, 8, 2, 9.
3/7/2014 11:38:40 DEBUG MESSAGE: Random number: 8, and more are: 8, 8, 9, 3, 4.
3/7/2014 12:09:40 DEBUG MESSAGE: Random number: 1, and more are: 6, 2, 9, 6, 5.
3/7/2014 12:40:40 DEBUG MESSAGE: Random number: 1, and more are: 6, 2, 9, 6, 5.

So, Smarty, you have a good solution.

I'm going to let this run longer just to see what happens.

Thank you.

Back to Top View gg102's Profile Search for other posts by gg102
 
dhoward
Admin Group
Admin Group
Avatar

Joined: June 29 2001
Location: United States
Online Status: Offline
Posts: 4447
Posted: March 07 2014 at 14:23 | IP Logged Quote dhoward

Yep, doing research on this issue and it seems that the built in rand function is not so random. Ive had several complaints over the years and in my own use, have seen this less than random behaviour.

Anyways, Im creating a new ph_random function that will use the Microsoft Cryptographic library which should provide a much more random set of numbers. It'll be included in the alpha.

gg,

The scary thing about your data is the data from 31 minutes. 1,6,2,9,6,5 is repeated 3 times. Definitely not random and proof of a problem. In the meantime, I wonder if it would make a difference if the randomize function is called just prior to calling rand (for a given block of random #'s...not before every call to rand). Something like:

10 Formula randomize(0)

20 set global "var2" rand(10)

30 ph_writefile("c:\random.txt",0, today() + " DEBUG MESSAGE: Random number: {VAR2}, and more are: " +
rand(10)+ ", "+rand(10)+ ", "+rand(10)+", "+rand(10)+
", "+rand(10)+ "." +char(13) + char(10) )

Dave.
Back to Top View dhoward's Profile Search for other posts by dhoward Visit dhoward's Homepage
 
gg102
Senior Member
Senior Member


Joined: January 29 2013
Location: United States
Online Status: Offline
Posts: 245
Posted: March 07 2014 at 14:32 | IP Logged Quote gg102

Dave, yes,

I'll try your idea and post the results either later today, or early tomorrow.

Thanks.
Back to Top View gg102's Profile Search for other posts by gg102
 
gg102
Senior Member
Senior Member


Joined: January 29 2013
Location: United States
Online Status: Offline
Posts: 245
Posted: March 07 2014 at 17:52 | IP Logged Quote gg102

Well, I let it run for a couple hours using the randomize(0) and got the following results:

added RANDOMIZE(0)
3/7/2014 13:42:41 DEBUG MESSAGE: Random number: 10, and more are: 6, 8, 7, 9, 10.
3/7/2014 14:13:40 DEBUG MESSAGE: Random number: 3, and more are: 5, 7, 4, 9, 6.
3/7/2014 14:44:40 DEBUG MESSAGE: Random number: 6, and more are: 6, 8, 2, 5, 7.
3/7/2014 15:15:40 DEBUG MESSAGE: Random number: 10, and more are: 3, 9, 9, 6, 2.
3/7/2014 15:46:40 DEBUG MESSAGE: Random number: 3, and more are: 2, 2, 8, 8, 6.
3/7/2014 16:17:40 DEBUG MESSAGE: Random number: 7, and more are: 5, 8, 6, 1, 7.
3/7/2014 16:48:40 DEBUG MESSAGE: Random number: 1, and more are: 1, 2, 3, 8, 5.

So, it appears that it's important to "stir the pot" before using the rand() function. It's still not widely varied, but this is better.

Thanks for your input Dave.

Back to Top View gg102's Profile Search for other posts by gg102
 
gg102
Senior Member
Senior Member


Joined: January 29 2013
Location: United States
Online Status: Offline
Posts: 245
Posted: March 07 2014 at 18:45 | IP Logged Quote gg102

Dave,

I'm still missing why time is a factor in the rand() function. As I showed in the first group of data, when the interval was 10 minutes, the rand() function was reasonably random, but when the interval was 31 minutes, the rand() function seemed to fizzle out and get "stuck."

I totally appreciate the problem with a rand() function being random, but why does time effect it?   Shouldn't it run the same no matter when it's called?

Is it something "technical?"


Back to Top View gg102's Profile Search for other posts by gg102
 
nick7920
Senior Member
Senior Member


Joined: March 04 2008
Location: United States
Online Status: Offline
Posts: 193
Posted: March 07 2014 at 19:04 | IP Logged Quote nick7920

I had same problem in different programing environment long time a ago but did you try something like this.

number(right(string(int(ph_seconds(now()))),1))

basically times in seconds - then take the right most number.

just an Idea.....

Nick
Back to Top View nick7920's Profile Search for other posts by nick7920
 
gg102
Senior Member
Senior Member


Joined: January 29 2013
Location: United States
Online Status: Offline
Posts: 245
Posted: March 07 2014 at 19:37 | IP Logged Quote gg102

nick,

Yes, and that's a good solution. Thank you for offering it. Basically, it's a mod 10 of seconds of now() for a number between 1 and 10. That works nicely. There are other similar techniques to get a narrow range random number. (Which BTW, I'm currently using as a work-around.)

Originally, I was just asking if anyone had the same experience, or if it was just me missing something or doing something stupid; again.

While I'm certainly NOT seeking to add to Dave's workload, I do like to be thorough when getting Dave involved so as to NOT send him in pursuit of untrained aquatic fowl.



Back to Top View gg102's Profile Search for other posts by gg102
 
smarty
Super User
Super User
Avatar

Joined: May 21 2006
Location: United States
Online Status: Offline
Posts: 728
Posted: March 08 2014 at 02:47 | IP Logged Quote smarty

"Pursuit of untrained aquatic fowl."..... Needed that laugh!

__________________
Elk - Insteon - BlueIris - DMC1 - PowerHome - XLobby - HA_Bridge w/Dots - Brultech
Back to Top View smarty's Profile Search for other posts by smarty
 

If you wish to post a reply to this topic you must first login
If you are not already registered you must first register

  Post ReplyPost New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum