Page 1 of 1

math.rand gives same results [solved]

Posted: Friday 05 April 2024 17:39
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

Re: math.rand gives same results

Posted: Saturday 06 April 2024 18:10
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
}


Re: math.rand gives same results

Posted: Monday 08 April 2024 13:10
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.

Re: math.rand gives same results

Posted: Monday 08 April 2024 15:19
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	


Re: math.rand gives same results

Posted: Monday 08 April 2024 16:54
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.

Re: math.rand gives same results

Posted: Tuesday 09 April 2024 8:42
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.

Re: math.rand gives same results

Posted: Tuesday 09 April 2024 8:45
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.

Re: math.rand gives same results

Posted: Tuesday 09 April 2024 8:49
by ravaoo
@ Moderator: leecollings
How do i set a subject to [solved]? Or is this a moderator thing?