math.rand gives same results [solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
ravaoo
Posts: 39
Joined: Friday 14 October 2022 10:46
Target OS: NAS (Synology & others)
Domoticz version: 2024-7
Location: Nederland
Contact:

math.rand gives same results [solved]

Post by ravaoo »

We have 6 colored lamps in the bathroom. For my kids i want to program a disco mode.
I have 7 defined colors and with math.rand(1,7) for each lamp this should give diferent colors for every lamp (or sometimes the same bij change).
The outcome is that every time most of the lamps have the same color.
On sites about LUA I learned there is a math.randomseed(xxx) to prevent the same results.
With or without math.randomseed results in same colors.

I think calling math.rand in the same second is the reason for giving same numbers. For disco effect i need to do this in a second. 8-)
Is it posible to get real random numbers?

Code: Select all

 local hueRoze=53201
        local hueBlauw=46793
        local hueRood=53201
        local hueOranje=65451
        local hueGeel=5159
        local hueGroen=26035
        local hueWit=41490
        local hueWarmwit=8276
        local kleur=1
math.randomseed(os.clock()*100000000000)
        -- lamp 1
        local deUrl='https://1.2.3.4/api/huekey/lights/18/state'

        -- Kies een kleur
                kleur=math.random(1, 7)
                      domoticz.log('Kleur = '..kleur)
      		    if (kleur==1) then
      			    postD = { on = true, sat = 254 , bri = 254 , hue = hueRoze }
                end 
                if (kleur==2) then
    			    postD = { on = true, sat = 254 , bri = 254 , hue = hueBlauw }
                end	    
        	    if (kleur==3) then
      			    postD = { on = true, sat = 254 , bri = 254 , hue = hueRood }
                end 
                if (kleur==4) then
    			    postD = { on = true, sat = 254 , bri = 254 , hue = hueOranje }
                end	    
        	    if (kleur==5) then
      			    postD = { on = true, sat = 254 , bri = 254 , hue = hueGeel }
                end 
                if (kleur==6) then
    			    postD = { on = true, sat = 254 , bri = 254 , hue = huegroen }
                end	    
        	    if (kleur==7) then
      			    postD = { on = true, sat = 78 , bri = 254 , hue = hueWit }
                end 
		
      			domoticz.openURL({
                url = deUrl,
				method = 'PUT',
  			    postData = postD,
				callback = 'return', -- see httpResponses above.
			})
			
        -- lamp 2
        deUrl='https://12.3.4/api/huekey/lights/19/state'

        -- Kies een kleur
                kleur=math.random(1, 7)
                      domoticz.log('Kleur = '..kleur)
      		    if (kleur==1) then
      			    postD = { on = true, sat = 254 , bri = 254 , hue = hueRoze }
                end 
                if (kleur==2) then
    			    postD = { on = true, sat = 254 , bri = 254 , hue = hueBlauw }
                end	    
        	    if (kleur==3) then
      			    postD = { on = true, sat = 254 , bri = 254 , hue = hueRood }
                end 
                if (kleur==4) then
    			    postD = { on = true, sat = 254 , bri = 254 , hue = hueOranje }
                end	    
        	    if (kleur==5) then
      			    postD = { on = true, sat = 254 , bri = 254 , hue = hueGeel }
                end 
                if (kleur==6) then
    			    postD = { on = true, sat = 254 , bri = 254 , hue = huegroen }
                end	    
        	    if (kleur==7) then
      			    postD = { on = true, sat = 78 , bri = 254 , hue = hueWit }
                end 
		
      			domoticz.openURL({
                url = deUrl,
				method = 'PUT',
  			    postData = postD,
				callback = 'return', -- see httpResponses above.
			})

        -- lamp 3
        deUrl='https://1.2.3.4/api/huekey/lights/20/state'

        -- Kies een kleur
                kleur=math.random(1, 7)
                      domoticz.log('Kleur = '..kleur)
      		    if (kleur==1) then
      			    postD = { on = true, sat = 254 , bri = 254 , hue = hueRoze }
                end 
                if (kleur==2) then
    			    postD = { on = true, sat = 254 , bri = 254 , hue = hueBlauw }
                end	    
        	    if (kleur==3) then
      			    postD = { on = true, sat = 254 , bri = 254 , hue = hueRood }
                end 
                if (kleur==4) then
    			    postD = { on = true, sat = 254 , bri = 254 , hue = hueOranje }
                end	    
        	    if (kleur==5) then
      			    postD = { on = true, sat = 254 , bri = 254 , hue = hueGeel }
                end 
                if (kleur==6) then
    			    postD = { on = true, sat = 254 , bri = 254 , hue = huegroen }
                end	    
        	    if (kleur==7) then
      			    postD = { on = true, sat = 78 , bri = 254 , hue = hueWit }
                end 
		
      			domoticz.openURL({
                url = deUrl,
				method = 'PUT',
  			    postData = postD,
				callback = 'return', -- see httpResponses above.
			})

        -- lamp 4
        deUrl='https://1.2.3.4/api/huekey/lights/21/state'

        -- Kies een kleur

                kleur=math.random(1, 7)
                      domoticz.log('Kleur = '..kleur)
      		    if (kleur==1) then
      			    postD = { on = true, sat = 254 , bri = 254 , hue = hueRoze }
                end 
                if (kleur==2) then
    			    postD = { on = true, sat = 254 , bri = 254 , hue = hueBlauw }
                end	    
        	    if (kleur==3) then
      			    postD = { on = true, sat = 254 , bri = 254 , hue = hueRood }
                end 
                if (kleur==4) then
    			    postD = { on = true, sat = 254 , bri = 254 , hue = hueOranje }
                end	    
        	    if (kleur==5) then
      			    postD = { on = true, sat = 254 , bri = 254 , hue = hueGeel }
                end 
                if (kleur==6) then
    			    postD = { on = true, sat = 254 , bri = 254 , hue = huegroen }
                end	    
        	    if (kleur==7) then
      			    postD = { on = true, sat = 78 , bri = 254 , hue = hueWit }
                end 
		
      			domoticz.openURL({
                url = deUrl,
				method = 'PUT',
  			    postData = postD,
				callback = 'return', -- see httpResponses above.
			})

        -- lamp 5
        deUrl='https://1.2.3.4/api/huekey/lights/23/state'

        -- Kies een kleur

                kleur=math.random(1, 7)
                      domoticz.log('Kleur = '..kleur)
      		    if (kleur==1) then
      			    postD = { on = true, sat = 254 , bri = 254 , hue = hueRoze }
                end 
                if (kleur==2) then
    			    postD = { on = true, sat = 254 , bri = 254 , hue = hueBlauw }
                end	    
        	    if (kleur==3) then
      			    postD = { on = true, sat = 254 , bri = 254 , hue = hueRood }
                end 
                if (kleur==4) then
    			    postD = { on = true, sat = 254 , bri = 254 , hue = hueOranje }
                end	    
        	    if (kleur==5) then
      			    postD = { on = true, sat = 254 , bri = 254 , hue = hueGeel }
                end 
                if (kleur==6) then
    			    postD = { on = true, sat = 254 , bri = 254 , hue = huegroen }
                end	    
        	    if (kleur==7) then
      			    postD = { on = true, sat = 78 , bri = 254 , hue = hueWit }
                end 
		
      			domoticz.openURL({
                url = deUrl,
				method = 'PUT',
  			    postData = postD,
				callback = 'return', -- see httpResponses above.
			})

        -- lamp 6
        deUrl='https://1.2.3.4/api/huekey/lights/24/state'

        -- Kies een kleur
                kleur=math.random(1, 7)
                      domoticz.log('Kleur = '..kleur)
      		    if (kleur==1) then
      			    postD = { on = true, sat = 254 , bri = 254 , hue = hueRoze }
                end 
                if (kleur==2) then
    			    postD = { on = true, sat = 254 , bri = 254 , hue = hueBlauw }
                end	    
        	    if (kleur==3) then
      			    postD = { on = true, sat = 254 , bri = 254 , hue = hueRood }
                end 
                if (kleur==4) then
    			    postD = { on = true, sat = 254 , bri = 254 , hue = hueOranje }
                end	    
        	    if (kleur==5) then
      			    postD = { on = true, sat = 254 , bri = 254 , hue = hueGeel }
                end 
                if (kleur==6) then
    			    postD = { on = true, sat = 254 , bri = 254 , hue = huegroen }
                end	    
        	    if (kleur==7) then
      			    postD = { on = true, sat = 78 , bri = 254 , hue = hueWit }
                end 
		
      			domoticz.openURL({
                url = deUrl,
				method = 'PUT',
  			    postData = postD,
				callback = 'return', -- see httpResponses above.
			})
To know: i changed the IP and huekey in the code
Last edited by ravaoo on Monday 29 April 2024 9:46, edited 1 time in total.
Running latest BETA on on Docker (Container Manager) in Synology NAS using zwave_js_ui with mqtt. Getting started with homebridge.

Living in an automated house is a survival but it is worth it
willemd
Posts: 642
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: math.rand gives same results

Post by willemd »

What if you run below script? Do you get different color numbers? I do.

Code: Select all

return {
	on = {
		timer = {
			'every minute',				-- causes the script to be called every minute
		}
	},
	logging = {
		level = domoticz.LOG_INFO,
		marker = 'template',
	},
	execute = function(domoticz, timer)
		domoticz.log('Timer event was triggered by ' .. timer.trigger, domoticz.LOG_INFO)
		for i = 1,100 do
		    kleur=math.random(1, 7)
            domoticz.log('Kleur = '..kleur)
        end
	end
}

ravaoo
Posts: 39
Joined: Friday 14 October 2022 10:46
Target OS: NAS (Synology & others)
Domoticz version: 2024-7
Location: Nederland
Contact:

Re: math.rand gives same results

Post by ravaoo »

willemd:
Tested your script
This script gif's me different color numbers!

Then i put your script in my script. Stil different color numbers in your script part and mine almost the same numbers!

Then I put in a for loop of 10 random numbers that i don't use for each lamp. Debugging de list of numbers that i don't use gives me 6 times (number of lamp's) the same row of random numbers.

At last i changed for each lamp de number to loop for not used color numbers. This works

So:
lamp 1 got 1 not used random numbers
lamp 2 got 2 not used random numbers
lamp 1 got 3 not used random numbers
lamp 1 got 4 not used random numbers
lamp 1 got 5 not used random numbers
lamp 1 got 6 not used random numbers

Strange to get the same row of numbers by using math.random (i like to think this is a bug)
But i have created a workaround to willemd who set me on the way.
Running latest BETA on on Docker (Container Manager) in Synology NAS using zwave_js_ui with mqtt. Getting started with homebridge.

Living in an automated house is a survival but it is worth it
User avatar
boum
Posts: 135
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: math.rand gives same results

Post by boum »

Your use of math.randomseed seems… random. It might overflow integer value and result in a no-op. Or the same value rounded every time so same sequence of generated numbers. Ideally, you should use math.randomseed(os.time()) only once before the whole disco commands.

Also lots of redundancy in your code. Can be shortened to be more easy to read and modify:

Code: Select all

 local hueRoze=53201
        local hueBlauw=46793
        local hueRood=53201
        local hueOranje=65451
        local hueGeel=5159
        local hueGroen=26035
        local hueWit=41490
        local hueWarmwit=8276
        local kleurPost= {
      			    { on = true, sat = 254 , bri = 254 , hue = hueRoze },
    			    { on = true, sat = 254 , bri = 254 , hue = hueBlauw },
      			    { on = true, sat = 254 , bri = 254 , hue = hueRood },
    			    { on = true, sat = 254 , bri = 254 , hue = hueOranje },
      			    { on = true, sat = 254 , bri = 254 , hue = hueGeel },
    			    { on = true, sat = 254 , bri = 254 , hue = huegroen },
      			    { on = true, sat = 78 , bri = 254 , hue = hueWit },
        }
        local lamps = {
            'https://1.2.3.4/api/huekey/lights/18/state', -- lamp 1
	    'https://12.3.4/api/huekey/lights/19/state',         -- lamp 2
            'https://1.2.3.4/api/huekey/lights/20/state',        -- lamp 3
            'https://1.2.3.4/api/huekey/lights/21/state',        -- lamp 4
            'https://1.2.3.4/api/huekey/lights/23/state',        -- lamp 5
            'https://1.2.3.4/api/huekey/lights/24/state',        -- lamp 6
        }

        for i, deUrl in ipairs(lamps) do
            -- Kies een kleur
                local kleur=math.random(1, 7)
                domoticz.log('Kleur = '..kleur)
                local postD = kleurPost[kleur]
		
      		domoticz.openURL({
                	url = deUrl,
			method = 'PUT',
  			postData = postD,
			callback = 'return', -- see httpResponses above.
			})
	end	

User avatar
waltervl
Posts: 5846
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: math.rand gives same results

Post by waltervl »

additional, are you doing something with callback in OpenURL()? It is not clear from the script you are giving us.

Code: Select all

callback = 'return', -- see httpResponses above.
Because if you do react on the callback you have to make it unique eg returnlamp1, returnlamp2 etc.
Else Dzvents will react on the 'return' callback giving random callbacks.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
ravaoo
Posts: 39
Joined: Friday 14 October 2022 10:46
Target OS: NAS (Synology & others)
Domoticz version: 2024-7
Location: Nederland
Contact:

Re: math.rand gives same results

Post by ravaoo »

boum wrote: Monday 08 April 2024 15:19 Your use of math.randomseed seems… random. It might overflow integer value and result in a no-op. Or the same value rounded every time so same sequence of generated numbers. Ideally, you should use math.randomseed(os.time()) only once before the whole disco commands.

Also lots of redundancy in your code. Can be shortened to be more easy to read and modify:

Code: Select all

 local hueRoze=53201
        local hueBlauw=46793
        local hueRood=53201
        local hueOranje=65451
        local hueGeel=5159
        local hueGroen=26035
        local hueWit=41490
        local hueWarmwit=8276
        local kleurPost= {
      			    { on = true, sat = 254 , bri = 254 , hue = hueRoze },
    			    { on = true, sat = 254 , bri = 254 , hue = hueBlauw },
      			    { on = true, sat = 254 , bri = 254 , hue = hueRood },
    			    { on = true, sat = 254 , bri = 254 , hue = hueOranje },
      			    { on = true, sat = 254 , bri = 254 , hue = hueGeel },
    			    { on = true, sat = 254 , bri = 254 , hue = huegroen },
      			    { on = true, sat = 78 , bri = 254 , hue = hueWit },
        }
        local lamps = {
            'https://1.2.3.4/api/huekey/lights/18/state', -- lamp 1
	    'https://12.3.4/api/huekey/lights/19/state',         -- lamp 2
            'https://1.2.3.4/api/huekey/lights/20/state',        -- lamp 3
            'https://1.2.3.4/api/huekey/lights/21/state',        -- lamp 4
            'https://1.2.3.4/api/huekey/lights/23/state',        -- lamp 5
            'https://1.2.3.4/api/huekey/lights/24/state',        -- lamp 6
        }

        for i, deUrl in ipairs(lamps) do
            -- Kies een kleur
                local kleur=math.random(1, 7)
                domoticz.log('Kleur = '..kleur)
                local postD = kleurPost[kleur]
		
      		domoticz.openURL({
                	url = deUrl,
			method = 'PUT',
  			postData = postD,
			callback = 'return', -- see httpResponses above.
			})
	end	

Thanks boum, I dit try without use of math.randomseed and this gives random numbers.

I am stil learning to use lua and dsvents :) Nice optimization for the code! I do like the ipairs bit.
Running latest BETA on on Docker (Container Manager) in Synology NAS using zwave_js_ui with mqtt. Getting started with homebridge.

Living in an automated house is a survival but it is worth it
ravaoo
Posts: 39
Joined: Friday 14 October 2022 10:46
Target OS: NAS (Synology & others)
Domoticz version: 2024-7
Location: Nederland
Contact:

Re: math.rand gives same results

Post by ravaoo »

waltervl wrote: Monday 08 April 2024 16:54 additional, are you doing something with callback in OpenURL()? It is not clear from the script you are giving us.

Code: Select all

callback = 'return', -- see httpResponses above.
Because if you do react on the callback you have to make it unique eg returnlamp1, returnlamp2 etc.
Else Dzvents will react on the 'return' callback giving random callbacks.
Thanks again waltervl. You are right!
Creating code, debug, try something, debug, etc. gives a lot of code that should not be there. ;) Now i got it working i can cleanup.
Running latest BETA on on Docker (Container Manager) in Synology NAS using zwave_js_ui with mqtt. Getting started with homebridge.

Living in an automated house is a survival but it is worth it
ravaoo
Posts: 39
Joined: Friday 14 October 2022 10:46
Target OS: NAS (Synology & others)
Domoticz version: 2024-7
Location: Nederland
Contact:

Re: math.rand gives same results

Post by ravaoo »

@ Moderator: leecollings
How do i set a subject to [solved]? Or is this a moderator thing?
Running latest BETA on on Docker (Container Manager) in Synology NAS using zwave_js_ui with mqtt. Getting started with homebridge.

Living in an automated house is a survival but it is worth it
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest