Jump to content


Photo

Return random item of table


  • Please log in to reply
15 replies to this topic

#1 Raul (ulneiz)

Raul (ulneiz)

    Guruh Motha Fakka is Levitating and Knows Everything About Renoise Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPip
  • 1544 posts
  • Gender:Male
  • Location:Spain

Posted 29 September 2018 - 15:39

The table:

table_1 = { a = "erygert", b = "tujjksdfg", c = "ertertt" }

How to return a random string from the table?

 

For example, return --> "tujjksdfg"

 

or return -- > "erygert"

 

I search a equivalent to:

table_2 = { "a", "b", "c" }
print( table_2[ math.random(1, #table_2) ] )

return --> "b", or return --> "a"...

 

 

Any suggestions? Thank you!


Edited by Raul (ulneiz), 30 September 2018 - 17:57.

:excl: Development of my tools: KangarooX120SamRenderPhraseTouch  |  GT16-Colors (old)

 

:excl: My API wishlist R3.1 (updated 24 July 2017):

Spoiler

 

:excl: My Renoise 3.1 wishlist (updated 26 September 2017):

Spoiler

#2 danoise

danoise

    Probably More God or Borg Than Human Member

  • Renoise Team
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 6968 posts
  • Gender:Male
  • Location:Berlin
  • Interests:wildlife + urban trekking

Posted 29 September 2018 - 17:36

If your table is indeed using values as keys, you can't just use the hash sign to obtain the length. 

 

So the first step would be to convert the keys into a table, using table.keys

local t_keys = table.keys(table_1)
rprint(t_keys)

[1] =>  a
[2] =>  c
[3] =>  b

This could can now be treated like in your second example, with the "twist" that

you're looking up the entries in the original table 


return table_1[t_keys[math.random(1,#t_keys)]]

  • 4Tey and Raul (ulneiz) like this

Tracking with Stuff. API wishlist | Soundcloud


#3 dblue

dblue

    Dodgy Geezer

  • Admins
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5827 posts
  • Gender:Male
  • Location:Berlin
  • Interests:Code. Music. Graphics.

Posted 29 September 2018 - 18:16

It would be a lot easier if you used numerical table keys instead of alpha characters :)

But if you insist on this particular approach, then here's another potentially interesting method to consider, which simply converts the characters a/b/c into bytes that will work just fine with math.random()

(Note: It assumes that you'll always have a continuous set of keys ranging from a to z, with no gaps, no other weird characters, etc. If your keys are a bit more 'exotic' then this probably won't work without some additional hacking...)
 
local table = { a = "erygert", b = "tujjksdfg", c = "ertertt" }

-- Convert characters to bytes that we can pass into math.random()
local rnd_min = string.byte('a')
local rnd_max = string.byte('c')

-- Pick a random byte
local rnd_key = math.random(rnd_min, rnd_max)

-- Convert byte to character
local rnd_chr = string.char(rnd_key)

-- Get string from table
local rnd_str = table[rnd_chr]

  • 4Tey and Raul (ulneiz) like this

#4 4Tey

4Tey

    Big Masta Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPip
  • 535 posts
  • Gender:Male

Posted 29 September 2018 - 18:45

If your table is indeed using values as keys, you can't just use the hash sign to obtain the length.

True danoise, but you can get the length from table.count() as well?  (Okay, I imagine this is much slower (function call overhead etc..) but I just thought I'd mention it as silly variation on a theme.)

table_1 = { a = "erygert", b = "tujjksdfg", c = "ertertt" }
print(table.values(table_1)[math.random(1,table.count(table_1))])

  • Raul (ulneiz) likes this

#5 joule

joule

    Guruh Motha Fakka is Levitating and Knows Everything About Renoise Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPip
  • 1837 posts
  • Gender:Not Telling

Posted 30 September 2018 - 12:17

The following was an interesting general approach that seems to be a lot faster.

function table.random( t )
    local choice = "F"
    local n, rnd = 0, math.random
    
    for i, o in pairs(t) do
        n = n + 1
        choice = rnd() < (1/n) and o or choice
    end
    return choice
end

tbl = { a = "erygert", b = "tujjksdfg", c = "ertertt" }

print(table.random(tbl))

Taken from https://stackoverflo...-a-table-in-lua


Edited by joule, 30 September 2018 - 12:19.

  • 4Tey and Raul (ulneiz) like this

#6 Raul (ulneiz)

Raul (ulneiz)

    Guruh Motha Fakka is Levitating and Knows Everything About Renoise Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPip
  • 1544 posts
  • Gender:Male
  • Location:Spain

Posted 30 September 2018 - 12:18

Thank you all!
local table = { a = "erygert", b = "tujjksdfg", c = "ertertt" }

-- Convert characters to bytes that we can pass into math.random()
local rnd_min = string.byte('a')
local rnd_max = string.byte('c')

-- Pick a random byte
local rnd_key = math.random(rnd_min, rnd_max)

-- Convert byte to character
local rnd_chr = string.char(rnd_key)

-- Get string from table
local rnd_str = table[rnd_chr]

Interesting. If I have not misunderstood, it is possible to use from "a" to "z" to do the random with this conversion. The fact of using a table, is by adding special characters, such as %@(= or similar.

 

 

 

table_1 = { a = "erygert", b = "tujjksdfg", c = "ertertt" }
print(table.values(table_1)[math.random(1,table.count(table_1))])

I do not know if it will be faster or not, but I was looking for something like this. The table that I use has about 70 items. It works fast with this code.

 

...

 

On the other hand, I am interested in any effective way of encryption or compilation of LUA code. But I have some doubts, for example if it affects the performance or if Renoise accepts some type of encryption for LUA code (files.lua). Maybe open another thread on this topic. I do not know, you imagine that you want to share a specific code with a partner to try it, but you do not want to read the code, or decrypt it (at least easily).


:excl: Development of my tools: KangarooX120SamRenderPhraseTouch  |  GT16-Colors (old)

 

:excl: My API wishlist R3.1 (updated 24 July 2017):

Spoiler

 

:excl: My Renoise 3.1 wishlist (updated 26 September 2017):

Spoiler

#7 Raul (ulneiz)

Raul (ulneiz)

    Guruh Motha Fakka is Levitating and Knows Everything About Renoise Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPip
  • 1544 posts
  • Gender:Male
  • Location:Spain

Posted 30 September 2018 - 12:24

@Joule. I will prove it.

 

Thanks! Have you ever encrypted or compiled an LUA file?


:excl: Development of my tools: KangarooX120SamRenderPhraseTouch  |  GT16-Colors (old)

 

:excl: My API wishlist R3.1 (updated 24 July 2017):

Spoiler

 

:excl: My Renoise 3.1 wishlist (updated 26 September 2017):

Spoiler

#8 dblue

dblue

    Dodgy Geezer

  • Admins
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5827 posts
  • Gender:Male
  • Location:Berlin
  • Interests:Code. Music. Graphics.

Posted 30 September 2018 - 12:32

it is possible to use from "a" to "z" to do the random with this conversion


I simply took the character "a" and used its decimal value instead.
  • a = 97
  • b = 98
  • c = 99
  • ...
See: ASCII

So the code is simply picking a random number between 97 and 99, and then converting that value back to the character "a", "b", or "c" in order to match your table keys.

The question for me remains: Why are you using alpha characters for table keys in the first place?

Why not use the "standard" numerical keys/indexes instead?

It sure is a lot easier to handle tables when the keys are simply numbered 1, 2, 3....

:)

 
local table = { "erygert", "tujjksdfg", "ertertt" }
local str = table[math.random(1, #table)]
print(str)
See? Nice and simple :)

#9 Raul (ulneiz)

Raul (ulneiz)

    Guruh Motha Fakka is Levitating and Knows Everything About Renoise Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPip
  • 1544 posts
  • Gender:Male
  • Location:Spain

Posted 30 September 2018 - 12:37

I simply took the character "a" and used its decimal value instead.

  • a = 97
  • b = 98
  • c = 99
  • ...
See: ASCII

So the code is simply picking a random number between 97 and 99, and then converting that value back to the character "a", "b", or "c" in order to match your table keys.

The question for me remains: Why are you using alpha characters for table keys in the first place?

Why not use the "standard" numerical keys/indexes instead?

It sure is a lot easier to handle tables when the keys are simply numbered 1, 2, 3....

:)

 

 

Yes, I know it's easier to use the numerical index. But I'm trying to get long strings of concrete characters. Maybe I can do it the other way around, converting a decimal value to a character, using this decimal value in the first place, for the index of the table.


:excl: Development of my tools: KangarooX120SamRenderPhraseTouch  |  GT16-Colors (old)

 

:excl: My API wishlist R3.1 (updated 24 July 2017):

Spoiler

 

:excl: My Renoise 3.1 wishlist (updated 26 September 2017):

Spoiler

#10 4Tey

4Tey

    Big Masta Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPip
  • 535 posts
  • Gender:Male

Posted 30 September 2018 - 12:54

I do not know, you imagine that you want to share a specific code with a partner to try it, but you do not want to read the code, or decrypt it (at least easily).

Hmm, a top top secret vertical piano roll? :)
 
I think Cie byte compiles his tool(s) Raul -> http://forum.renoise...step-sequencer/

 

Now don't ask me, I've never done it personally.  (I'm pretty certain that danoise wouldn't want to plagiarize my awful code anyway :) )   



#11 Raul (ulneiz)

Raul (ulneiz)

    Guruh Motha Fakka is Levitating and Knows Everything About Renoise Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPip
  • 1544 posts
  • Gender:Male
  • Location:Spain

Posted 30 September 2018 - 14:09

Hmm, a top top secret vertical piano roll? :)
 
I think Cie byte compiles his tool(s) Raul -> http://forum.renoise...step-sequencer/

 

I am not an expert on these issues. I understand that if you only encrypt the original code (a file.lua), then you can do it in reverse on the encrypted code to get the original code again. That is, any expert user can obtain the original code "easily".

 
On the other hand, if the code is compiled, it is not possible to obtain it in reverse. I still do not know how to make all this effective and safe. I'm also worried if the fact of using an encrypted code influences the real performance of the tool.
 
So, I still do not know how to differentiate between encrypting and copying, and the methods that exist, and even less if they are compatible with Renoise, that is, if Renoise can load the tool without problem, without any initial error because of the encryption / compilation.
 

 

 

Now don't ask me, I've never done it personally.  (I'm pretty certain that danoise wouldn't want to plagiarize my awful code anyway  :) )   

 

^_^  ^_^  ^_^ Some time ago I read in a forum of Renoise that maybe it is appropriate to encrypt the code to hide how messy and disastrous it can be and that people only notice how it works, not how it is written...


Edited by Raul (ulneiz), 30 September 2018 - 14:13.

:excl: Development of my tools: KangarooX120SamRenderPhraseTouch  |  GT16-Colors (old)

 

:excl: My API wishlist R3.1 (updated 24 July 2017):

Spoiler

 

:excl: My Renoise 3.1 wishlist (updated 26 September 2017):

Spoiler

#12 4Tey

4Tey

    Big Masta Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPip
  • 535 posts
  • Gender:Male

Posted 30 September 2018 - 15:25

So, I still do not know how to differentiate between encrypting and copying, and the methods that exist, and even less if they are compatible with Renoise, that is, if Renoise can load the tool without problem, without any initial error because of the encryption / compilation.

I wouldn't of said that the byte code version makes too much difference to Renoise.  After all, when you execute a tool, lua takes the source code, parses/byte compiles it internally and then runs that through the lua VM I do believe.  All you are doing is cutting out/down the parsing side AFAIK.

 

Hmm, next we could be expecting a 'you have 30 days of evaluation left...after that pay up!' dialog with your new tool Raul? ;) :) Oh man, Renoise lua tools that are fully-licensed-commercial-closed-source.



#13 Raul (ulneiz)

Raul (ulneiz)

    Guruh Motha Fakka is Levitating and Knows Everything About Renoise Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPip
  • 1544 posts
  • Gender:Male
  • Location:Spain

Posted 30 September 2018 - 15:52

I wouldn't of said that the byte code version makes too much difference to Renoise.  After all, when you execute a tool, lua takes the source code, parses/byte compiles it internally and then runs that through the lua VM I do believe.  All you are doing is cutting out/down the parsing side AFAIK.

 

Hmm, next we could be expecting a 'you have 30 days of evaluation left...after that pay up!' dialog with your new tool Raul? ;) :) Oh man, Renoise lua tools that are fully-licensed-commercial-closed-source.

 

You've got much imagination! First I would have to learn to master this issue of protecting LUA files...

 

Now that you mention it, it does not seem that there are many people interested in making tools for Renoise, neither enough "big" or good enough to sell them. Neither does it seem that people are willing to pay for it, even if it is a pittance to support the developer, let alone donate a small quantity (of your consideration). Moreover, if you notice, many people download free tools or other free content and not even leave a message of appreciation of 2 words. It is very sad.

 

Regardless of all this, I would like to have some control of everything I do.

:excl: Development of my tools: KangarooX120SamRenderPhraseTouch  |  GT16-Colors (old)

 

:excl: My API wishlist R3.1 (updated 24 July 2017):

Spoiler

 

:excl: My Renoise 3.1 wishlist (updated 26 September 2017):

Spoiler

#14 4Tey

4Tey

    Big Masta Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPip
  • 535 posts
  • Gender:Male

Posted 30 September 2018 - 18:11

Now that you mention it, it does not seem that there are many people interested in making tools for Renoise, neither enough "big" or good enough to sell them. Neither does it seem that people are willing to pay for it, even if it is a pittance to support the developer, let alone donate a small quantity (of your consideration). Moreover, if you notice, many people download free tools or other free content and not even leave a message of appreciation of 2 words. It is very sad.

Ah, the idea that there is no incentive to publish tools to this 'community' as there is no appreciation anyway and if Taktik isn't bothered working on Renoise then why should we?  I'll be truthful with you Raul, I have just one 'tool' installed.  It's called Nibbles.  Why?  No reason, I just happened to of coded some of it at the time.  I don't use the 'tool' as it isn't really a great music production tool anyway :)   You've written 'Kangaroo' tool Raul.  I haven't said anything about it because it is not a tool I would use.  Same with your GT-16.  Just because I don't use the tools you've written doesn't mean that I don't know/appreciate how much work you've put into them.  On the contrary.  So when you wrote 'Kangaroo' and posted it here Raul, people will take it, look at it, maybe use it/maybe delete it.  Maybe said person might come back to you and say 'Oh thanks for this tool Raul!  Bye!' and maybe thumb up your post.  That's it.  All that work and effort just for that.  If somebody does come back with something else it'll probably be along the lines of 'Oh Raul, nice tool, but could you just....' or 'It'll be much much better if it did....' or 'This crashes...'  Is it worth it?  Up to you to decide if you publish the tool on a forum, depends on the person/coder and how much you expect in return.  As you say, not many people (in recent times anyway, apart from say danoise) write/publish lua tools with/for Renoise on this forum.  I don't know Raul, maybe simply people also don't want to get involved.  It's a complicated thing.  People are tiring, difficult and they are right and everybody else is wrong etc..  In a nutshell I don't expect a lot from this forum in return myself.



#15 Raul (ulneiz)

Raul (ulneiz)

    Guruh Motha Fakka is Levitating and Knows Everything About Renoise Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPip
  • 1544 posts
  • Gender:Male
  • Location:Spain

Posted 30 September 2018 - 18:37

Ah, the idea that there is no incentive to publish tools to this 'community' as there is no appreciation anyway and if Taktik isn't bothered working on Renoise then why should we?  I'll be truthful with you Raul, I have just one 'tool' installed.  It's called Nibbles.  Why?  No reason, I just happened to of coded some of it at the time.  I don't use the 'tool' as it isn't really a great music production tool anyway :)   You've written 'Kangaroo' tool Raul.  I haven't said anything about it because it is not a tool I would use.  Same with your GT-16.  Just because I don't use the tools you've written doesn't mean that I don't know/appreciate how much work you've put into them.  On the contrary.  So when you wrote 'Kangaroo' and posted it here Raul, people will take it, look at it, maybe use it/maybe delete it.  Maybe said person might come back to you and say 'Oh thanks for this tool Raul!  Bye!' and maybe thumb up your post.  That's it.  All that work and effort just for that.  If somebody does come back with something else it'll probably be along the lines of 'Oh Raul, nice tool, but could you just....' or 'It'll be much much better if it did....' or 'This crashes...'  Is it worth it?  Up to you to decide if you publish the tool on a forum, depends on the person/coder and how much you expect in return.  As you say, not many people (in recent times anyway, apart from say danoise) write/publish lua tools with/for Renoise on this forum.  I don't know Raul, maybe simply people also don't want to get involved.  It's a complicated thing.  People are tiring, difficult and they are right and everybody else is wrong etc..  In a nutshell I don't expect a lot from this forum in return myself.

 

I speak in general, not of my tools (which by the way, are recent). If you look at some veteran tools, they have thousands of downloads, and then you read the linked forum and the vast majority of people do not appreciate it. They make the act of downloading because it is "free". And it is over. Deep down, this does not matter to me. But I am also sad that this is the philosophy. People take (download) and then do not thank, instead of thanking first, and then download. At least say hello, fuck!  ^_^

 

By the way, what's happening to you lately? I notice you more disappointed than usual.


:excl: Development of my tools: KangarooX120SamRenderPhraseTouch  |  GT16-Colors (old)

 

:excl: My API wishlist R3.1 (updated 24 July 2017):

Spoiler

 

:excl: My Renoise 3.1 wishlist (updated 26 September 2017):

Spoiler

#16 4Tey

4Tey

    Big Masta Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPip
  • 535 posts
  • Gender:Male

Posted 30 September 2018 - 19:39

By the way, what's happening to you lately? I notice you more disappointed than usual.

Maybe a little more disappointed than usual I suppose, I'm trying to write a simple program and maybe a tune, but I can't seem to concentrate on either somehow.  But, I'm still going Raul :)