Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


  • 0

math.random behaves wrong in VDs vs Scenes


Question

Posted

I have just found that using the math.random function does not work correctly in VDs (Virtual Devices). It reuses the SAME random seed for EVERY call!

 

So if one uses math.random in a scene it will always generate a new random number.

 

BUT in a VD, it will always generate the SAME value.

 

Can easily be tested with:

Please login or register to see this code.

17 answers to this question

Recommended Posts

  • 0
Posted

Please login or register to see this link.

 

Looks like working to me, tested it under the main loop.

  • 0
Guest JaapK
Posted

I get the same 'random' number too.

The same seed is used every time.

 

The solution is to add the following code above your loop:

math.randomseed(os.time())

  • 0
Posted

What software do you have? I was not able to reproduce this error on our gear.

  • 0
  • Inquirer
  • Posted

    I have 3.600 - and I have a friend with 4.42.

     

    Regarding VDs: Do not run it in the main loop - add a key, and put the code in the key - each time you press that key it will generate the same random numbers.

    • 0
    Posted

    In 4.042 it gives me proper results... Does this happen every time?

    • 0
    Posted

    I have the problem and use 4.042

    Please login or register to see this code.

    • 0
    Posted

    Are you using any other LUA script?

     

    Something only somebody from Fibaro might answer:

    Or is there any other LUA script running or setting that is initialising random number generator via randomseed the wrong way?

    • 0
    Posted

    I have 3.600 - and I have a friend with 4.42.

     

    Regarding VDs: Do not run it in the main loop - add a key, and put the code in the key - each time you press that key it will generate the same random numbers.

     

    Hahahahahahaha. So funny that it's always the same 10 random numbers.

    • 0
    Posted

    I will report this behavior, generator must indeed be initialized with the same number all over again.

    • 0
    Posted

    I will report this behavior, generator must indeed be initialized with the same number all over again.

    How does this going, still the same when i run math.random

    i use latest Beta

     

    [DEBUG] 20:49:59: random: 40 [DEBUG] 20:50:22: random: 40 [DEBUG] 20:56:44: random: 40
    [DEBUG] 20:49:59: random: 79 [DEBUG] 20:50:22: random: 79 [DEBUG] 20:56:44: random: 79
    [DEBUG] 20:49:59: random: 80 [DEBUG] 20:50:22: random: 80 [DEBUG] 20:56:44: random: 80
    [DEBUG] 20:49:59: random: 92 [DEBUG] 20:50:22: random: 92 [DEBUG] 20:56:44: random: 92
    [DEBUG] 20:49:59: random: 20 [DEBUG] 20:50:22: random: 20 [DEBUG] 20:56:44: random: 20
    [DEBUG] 20:49:59: random: 34 [DEBUG] 20:50:22: random: 34 [DEBUG] 20:56:44: random: 34
    [DEBUG] 20:49:59: random: 77 [DEBUG] 20:50:22: random: 77 [DEBUG] 20:56:44: random: 77
    [DEBUG] 20:49:59: random: 28 [DEBUG] 20:50:22: random: 28 [DEBUG] 20:56:44: random: 28
    [DEBUG] 20:49:59: random: 56 [DEBUG] 20:50:22: random: 56 [DEBUG] 20:56:44: random: 56
    • 0
    Posted

    I think this works better:

    Please login or register to see this code.

    Output from main loop:

    Please login or register to see this code.

    Or is that not what you meant? Maybe I've misunderstood the question...
    • 0
    Posted

    Put it in a vd and in a button not in main loop and you get the same result every time.

    • 0
    Posted

    Ah! I bet that only happens when you have less than one second between each button press? Thats because os.time() has 1 second resolution. In main loop, there's a 3 second interval. I need to think about this one...

    • 0
    Posted

    I got sevrel minutes between if you look in my post a bit up in this thread.

    • 0
    Posted

    ... don't think I have that problem. But If I click too fast, I get the effect I described, not ideal anyway. It's late... I'll try again later, when I'm awake

    Please login or register to see this image.

    /emoticons/default_icon_wink.gif" alt=";-)" />

    • 0
    Posted

    I use that you recomended and it works fine when i use math.randomseed(os.time())

    But why doesent it work without os.time?

    • 0
    Posted

    It is called a pseudo-random generator. That means... It is actually a mathematical formula, not random. The output looks random, and it has statistical properties, but it's a sequence. This formula, or sequence, has a starting point, aka seed.

    You set the starting point by calling math.randomseed(a_number). If you do math.randomseed(12345) on your HC2, and I do the same on mine, they both start producing the same sequence. The trick is to feed it something that changes... like os.time() - that's just a big number, that changes often.

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Answer this question...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...