Home › Forums › General › Programming › randomize a text label?
- This topic has 36 replies, 3 voices, and was last updated 6 years, 5 months ago by human fly.
-
AuthorPosts
-
October 29, 2017 at 7:26 am #73324
i would like to randomize a ‘preset name’ label, 10 characters.
here are some elements, i’m trying to figure out how to proceed
(there’s a little panel set up for it). there are ‘end’ markers missing,
and it won’t work yet.
(some old stuff commented out)function createRandomName() -- create a random preset name -- This variable stops index issues during panel bootup if panel:getRestoreState() == true or panel:getProgramState() == true then return end tabl_NewName={} -- Retrieving preset Name presetName = L(panel:getModulatorByName("lcd_presetName"):getComponent():getProperty("uiLabelText")) -- Store each character in variable and convert it from ASCII to numerical code name1 = string.byte(presetName,1) name2 = string.byte(presetName,2) name3 = string.byte(presetName,3) name4 = string.byte(presetName,4) name5 = string.byte(presetName,5) name6 = string.byte(presetName,6) name7 = string.byte(presetName,7) name8 = string.byte(presetName,8) name9 = string.byte(presetName,9) name10 = string.byte(presetName,10) --name characters as params if name1 == nil then name1 = "32" end tabl_NewName[1] = string.format("%.2x", name1) if name2 == nil then name2 = "32" end tabl_NewName[2] = string.format("%.2x", name2) if name3 == nil then name3 = "32" end tabl_NewName[3] = string.format("%.2x", name3) if name4 == nil then name4 = "32" end tabl_NewName[4] = string.format("%.2x", name4) if name5 == nil then name5 = "32" end tabl_NewName[5] = string.format("%.2x", name5) if name6 == nil then name6 = "32" end tabl_NewName[6] = string.format("%.2x", name6) if name7 == nil then name7 = "32" end tabl_NewName[7] = string.format("%.2x", name7) if name8 == nil then name8 = "32" end tabl_NewName[8] = string.format("%.2x", name8) if name9 == nil then name9 = "32" end tabl_NewName[9] = string.format("%.2x", name9) if name10 == nil then name10 = "32" end tabl_NewName[10] = string.format("%.2x", name10) -- random seeder local t=os.time() local c=os.clock() math.randomseed(t*c) -- items to randomize -- for i=1,10 etc --rnd=math.random(32,127) --value range to random etc end --concatenate table into single string DatasConcat = table.concat(tabl_NewName, " ", 1, 10) console ("DatasConcat : "..DatasConcat) --send it to preset name lcd panel:getComponent("lcd_presetName"):setPropertyString("uiLabelText",""..DatasConcat) end
Attachments:
You must be logged in to view attached files.October 31, 2017 at 6:22 am #73352maybe more something like this? missing how to apply the random:
(at the moment, it can get the bytes individually and put them back
together again)function createRandomName() -- create a random preset name -- This variable stops index issues during panel bootup if panel:getRestoreState() == true or panel:getProgramState() == true then return end presetName = L(panel:getModulatorByName("lcd_presetName"):getComponent():getProperty("uiLabelText")) console("Retreiving preset name from lcd_presetName : "..presetName) -- Store each character in variable local name1 = string.byte(presetName,1) local name2 = string.byte(presetName,2) local name3 = string.byte(presetName,3) local name4 = string.byte(presetName,4) local name5 = string.byte(presetName,5) local name6 = string.byte(presetName,6) local name7 = string.byte(presetName,7) local name8 = string.byte(presetName,8) local name9 = string.byte(presetName,9) local name10 = string.byte(presetName,10) -- random seeder local t=os.time() local c=os.clock() math.randomseed(t*c) -- items to randomize tabl_NewName={} for j= 1,10 do rnd=math.random(32,127) --value range to random -- /////what here?///// --(rnd,false,false,false) -- table.insert(tabl_NewName,j) end -- put bytes in table as characters if name1 == nil then name1 = " " end tabl_NewName[1] = string.char(name1) if name2 == nil then name2 = " " end tabl_NewName[2] = string.char(name2) if name3 == nil then name3 = " " end tabl_NewName[3] = string.char(name3) if name4 == nil then name4 = " " end tabl_NewName[4] = string.char(name4) if name5 == nil then name5 = " " end tabl_NewName[5] = string.char(name5) if name6 == nil then name6 = " " end tabl_NewName[6] = string.char(name6) if name7 == nil then name7 = " " end tabl_NewName[7] = string.char(name7) if name8 == nil then name8 = " " end tabl_NewName[8] = string.char(name8) if name9 == nil then name9 = " " end tabl_NewName[9] = string.char(name9) if name10 == nil then name10 = " " end tabl_NewName[10] = string.char(name10) --concatenate table into single string DatasConcat = table.concat(tabl_NewName, "", 1, 10) console ("DatasConcat : "..DatasConcat) --send it to preset name lcd panel:getComponent("lcd_presetName"):setPropertyString("uiLabelText",""..DatasConcat) end
November 1, 2017 at 5:57 am #73358i’m stumped on this – do not know how to apply the ‘rnd’
to items ‘name1’ to ‘name10’. (‘rnd’ is the math.random
operation, with range 32,127)having trouble visualising how this is done: operation
on a variable – rather than a ctrlr modulator –‘string.byte’ is a number value, and prints to console,
so i’m thinking it should work if i get all the bytes as
numbers (ascii values), randomise them, then put them in
a table as characters (string.char), which is then concatenated,
and sent to the ‘preset name’ label.so far, the concatenation of the string.char table puts it back
together as it goes in, so that appears to work ok.November 1, 2017 at 7:02 am #73359ah. this gets the ascii number values:
for i= 1,10 do console(String(string.byte(presetName,i))) end
November 1, 2017 at 8:31 am #73360so this is where i’m up to. cut out unnecessary stuff, i think.
getting ‘attempt to call a number value (rnd)’ so i need to figure
out how to do that. it will get the bytes and send the number values
to a 2nd label:
edit: sorry: just noticed it is not showing square brackets with ‘i’
after i ok the code. weird. should be here:console(tostring(tabl_NewName))
(uploaded the panel)function createRandomName() -- create a random preset name -- This variable stops index issues during panel bootup if panel:getRestoreState() == true or panel:getProgramState() == true then return end presetName = L(panel:getModulatorByName("lcd_presetName"):getComponent():getProperty("uiLabelText")) console("presetName is: "..presetName) -- random seeder local t=os.time() local c=os.clock() math.randomseed(t*c) -- value range to random rnd=math.random(32,127) tabl_NewName={} for i= 1,10 do local s=L(String(string.byte(presetName,i))) table.insert(tabl_NewName,s) console(tostring(tabl_NewName)) -- prints values individually -- items to randomize --local randAscii = rnd(tabl_NewName) -- //attempt to call global 'rnd' (a number value)// end --concatenate table into single string DatasConcat = table.concat(tabl_NewName, " ", 1, 10) console ("DatasConcat : "..DatasConcat) --send it to preset name lcd panel:getComponent("lcd_presetNameBis"):setPropertyString("uiLabelText",""..DatasConcat) end
Attachments:
You must be logged in to view attached files.November 1, 2017 at 8:50 pm #73362if i do this, it randomizes but they all come out as
the same random number ( haven’t tried to randomize
character directly yet) – how would one get a separate
random number for each ‘i’ ?rnd=math.random(32,127) tabl_NewName={} for i= 1,10 do local s=L(String(string.byte(presetName,i))) table.insert(tabl_NewName,s) end for j=1,10 do tabl_NewName[j] = tostring(rnd) end console(tostring(table.concat(tabl_NewName," ")))
just tried string.char, it didn’t like it: ‘number expected’.
so the number/byte has to be randomized, and then it has to
be converted back to characters …November 1, 2017 at 9:27 pm #73363it appears i can also do (shorter, but still returns all
the same random number):-- value range to random rnd=math.random(32,127) tabl_NewName={} for i= 1,10 do local s=L(String(rnd,string.byte(presetName,i))) table.insert(tabl_NewName,s) end
that ‘rnd’ is in the wrong place…
…and on reflection, i think the whole expression is
wrong: it doesn’t need to randomize any source characters,
and isn’t doing it here, i think.what i need to do is generate a series of random integers
between (32,127), and then convert those to characters.
start again..November 1, 2017 at 10:10 pm #73364so this does the same thing…still all the same random number:
rnd=math.random(32,127) tabl_NewName={} for i= 1,10 do i= rnd,i table.insert(tabl_NewName,i) end
and this no good either:
for i= 1,10 do while i <= 10 do i= rnd,i end
so why isn’t it producing a new random at each ‘i’ ?
November 2, 2017 at 12:48 am #73367hi,
for now you randomize before you go into looping so the rnd value isn’t changing anymore while you are in the looping part. Try to put the randomization bit inside the loop so it will be randomized each time your loop iterate.
Cheers!November 2, 2017 at 7:02 am #73372ok, so you don’t mean this 🙂 :
for i= 1,10 do while i <= 10 do -- random seeder local t=os.time() local c=os.clock() math.randomseed(t*c) -- value range to random rnd=math.random(32,127) i= rnd,i end table.insert(tabl_NewName,i) end
(much)earlier, i had a completely different method that
was producing individual randoms, but with several decimal
points, like floats (no good for this).i think the example on the Lua wiki is just picking a
number between 1,10000 – but the reset of it produces
a list separated by commas – like this. confusing…so somehow i have to put it in the bit that goes ‘i=rnd,i’ ?
and then i could maybe get rid of the ‘while’ ?trying in vain to remember earlier random that i overwrote 🙁
this does something weird, don’t understand what, exactly:i= L(String(rnd,i))
lua result comes back as:
‘LUA>> 35.0 35.00 35.000 35.0000 35.00000 35.000000 35 35 35 35’(what’s going on there?)
edit: lots of stuff to read here:
http://lua-users.org/wiki/MathLibraryTutorialNovember 2, 2017 at 10:26 am #73373Why don’t you create a table with all the ASCII characters you need in it and just randomize thru the tables elements? I am pretty sure guys on the forum here posted this kind of ASCII characters tables many times already.
Cheers!November 2, 2017 at 10:42 am #73374mm, because that seems quite long to me and i think i already
have them with rnd=math.random(32,127) – plus i’m only using
them as a stage in a generation of random text, for 10characters.
-that’s the next fun bit: converting it back to text with
‘string.char’ (for which i’ll somehow have to define the
bytes to be turned into characters) – this seems to be my
best option.
and… this means of course that i may have characters i don’t
want… what, so you’re saying only list alphanumerical and space,
and random from that? ha: i still have to learn to random anyway,
right? 😉 (my one step at a time process…)November 2, 2017 at 11:12 am #73375in fact, what i want my button to do is *write the table*
each time i click, filling it with random integers between
32 and 127 (or whatever will be the useful range: have to check
the ascii list to see where the alphanumericals are).then, i want to get these as characters, and send them as a
string to the text label.the point i’ve seen made is that the randomizer is only getting
seeded once, so that gives the same number for all table items.so… what you are suggesting is that i should make an array/table
with all useful characters, and get the randomizer to pick from
that to build my newName table. mm. yeah, that could work quite
well, and then i don’t need to convert back, or have the complication
of excluding unwanted symbols. (lol, so lazy he can’t be bothered
to create patch names ! – well it is a hassle, isn’t it.. )i’d still like to figure out how to do it this way, just for the
hell of it.November 3, 2017 at 2:30 am #73376How about this?
-- Called when a modulator value changes -- @mod http://ctrlr.org/api/class_ctrlr_modulator.html -- @value new numeric value of the modulator -- function warmUp() math.randomseed(os.clock()) math.random(); math.random(); math.random() -- warmup end -- function -------------------------------------------------------------- myMethod = function(--[[ CtrlrModulator --]] mod, --[[ number --]] value, --[[ number --]] source) tab={} warmUp() -- generate randomseed for i= 1,10 do rnd=math.random(32,127) tab=rnd -- populate table tab with random numbers end shuffleTable = {} -- tab seems to repeat some of the positions with the same number so I re-shuffle them for i=1,10 do shuffleTable = tab[math.random(1,10)] end label="[ " for i,v in ipairs (shuffleTable) do console(String(v)) label=label..v.." " end label=label.." ]" panel:getComponent("modulator-2"):setPropertyString("uiLabelText",label) console(String("________________________")) end -- function --------------------------------------------------------------
Attachments:
You must be logged in to view attached files.November 3, 2017 at 6:20 am #73378great, you’ve solved it 🙂
i’ve kept my seeder – i saw the ‘warmup’ on one of the pages
i was looking at yesterday but i think os time*os clock is
pretty good (am familiar with patterns produced by pseudo random)so, unless i’m missing something, this appears to do the job:
(nice and short)
nb: once again, ‘code’ isn’t showing square bracket @ ‘tabl_NewName=rnd’function createRandomName() -- create a random preset name -- This variable stops index issues during panel bootup if panel:getRestoreState() == true or panel:getProgramState() == true then return end -- random seeder local t=os.time() local c=os.clock() math.randomseed(t*c) -- -------------------------------------------------- tabl_NewName={} for i= 1,10 do rnd=math.random(32,127) tabl_NewName=rnd end -- -------------------------------------------------- DatasConcat = table.concat(tabl_NewName, " ") console ("DatasConcat : "..DatasConcat) --send it to preset name lcd panel:getComponent("lcd_presetNameBis"):setPropertyString("uiLabelText",""..DatasConcat) end
okay: so next task is to convert that back to characters with string.char
-which i shall attempt presently. there will no doubt be some problem or
other with that..or maybe i’ll get lucky.- This reply was modified 6 years, 5 months ago by human fly.
Attachments:
You must be logged in to view attached files.November 3, 2017 at 6:55 am #73381Well done – that’s a very nice and concise function in that panel!
November 3, 2017 at 7:11 am #73382looool ! not without that little push ! thanks for providing
the key.now i’m having trouble getting table items back to
text form. i’ll have to do a bit of research, because
name1 == string.byte/char etc. is returning nil when
i try to print to console. (expression is wrong)it seems that going via the table is the way, since it
isolates each item nicely, so it should just be a matter
of treating that value as an ascii value, and turning it
into a character – ? –November 3, 2017 at 7:18 am #73384This will do the job – it’s from my JD990 panel
function myConvertToChar(myCharacter) return string.char(tonumber(myCharacter,10)) end --function ---------------------------------------------------------
November 3, 2017 at 7:21 am #73385ah ok: i will try that now.
i was doing this: (again, square brackets not showing)
local name1 = tostring(tabl_NewName) console ("name1 : "..name1)
it prints:
‘LUA>> name1 : nil’?
November 3, 2017 at 7:29 am #73386ah ok: i will try that now.
i was doing this: (again, square brackets not showing)
local name1 = tostring(tabl_NewName) console ("name1 : "..name1)
it prints:
‘LUA>> name1 : nil’?
I think you are trying to print a table there, which won’t work.
Here is your panel modified to print a random string to the uiLabel lcd_presetNameTer 🙂
Attachments:
You must be logged in to view attached files. -
AuthorPosts
- The forum ‘Programming’ is closed to new topics and replies.