oegma2
Hi, I am new to Elixir world and still have a long way to go, but starting with a basic problem I’ve been using to learn any new language - one with monkies hitting random keys and see if they can write a chapter out of Shakespeare’s book
- but in order to solve this problem, I need to generate a random letter, representing the monkey’s hitting a key…So after a bit of googling found an option using List and rand.uniform(…) to return a random “key” out of a fixed list
The problem is, doing this function over and over is a key component and is really slow compared to any other language… I know Elixir and the BEAM VM are designed for reliability and thus immutable obj can slow things down…
Is there any solution to speed this code up below, so that I can continue the journey with Elixir and write a program that can spawn million’s of monkies, all hitting keys
chars = ‘ABCDEFGHIJKLMNOPQRSTUVW’
data = List.to_tuple(chars)
for x ← 0..1000000 do
elem(data, :rand.uniform(22))
end
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
hauleth
Lists do not support random access and access is linear, so to get nth element you need
nsteps. In other words theEnum.at/2for list will be implemented as:oegma2
Thanks for the help, and learned very quickly that in func lang, recursive is your friend
- meaning you can’t do list[index] … that not gone work
I tried your solution above but seems slower (And tried as in I prbl done something horribly wrong using your code, but have to start somewhere :)) - used Benchee for my benchmark results
My results
benwilson512
He wasn’t providing a faster solution, he was showing how the code underlying your existing solution worked, demonstrating that it was O(N).
Probably the fastest thing is to just compute a random number between 0 and 22 and add the correct ASCII offset.
hauleth
Except I would write it as:
For less “magic numbers”. Also
:randis preferred solution over:random.However I am not sure if
Enum.random(?A..?Z)isn’t optimised for such case (and if not it probably would be nice addition).EDIT:
Enum.random(?A..?Z)will run in constant time and space, so it would be the best and the fastest solution.cc @oegma2
oegma2
Thanks, @hauleth, and @benwilson512 for the input - going to try the
Enum.random(?A..?Z),[:rand.uniform(?Z - ?A) + ?A)]and[:rand.uniform(?Z - ?A) + ?A)]Agree with @hauleth regarding the
:randis preferred solution over:random- valid point, but my use-case, don;t need true crypto style random numbers. But good to keep it in mindThanks so much for the help and quick response - really awesome community!
hauleth
For cryptographically secure rands you need to use
:crypto.random_bytes/1,:randisn’t crypto safe either.rvirding
Yes, this is explicitly mentioned in the module
randdocs:oegma2
So I’ve done some testing and found 2 options that pretty fast for our Monkey app hitting random keys. Thanks to @hauleth and @benwilson512 for providing me with test1 option below
@hauleth / @rvirding : Thanks for the info on
:randregarding crypto. For my example, since the monkies just need a random number, it don’t need super secure random numbers, just random. But solid info and will keep that in mind for future projects that require secure random numbers.Back to the monkies app - The strange part is that using tuple’s in this case with the Kernel
elem(tuple, index)seems to be a bit faster thanEnum.random(?A..?Z)I’ve asked the Monkies to try and type the following sequence in order “FOXYL” at random, and if they type FOD they start over - must be in perfect sequence, else they start from scratch. It’s actually scary to think a random monkey got that word in under 4 seconds in test1 and under 2 seconds in test2
Below is the results from Benchee
test1 using
Enum.random(?A..?Z)test2 using
hauleth
Ok, I have tested on my own, and to my surprise you are right.
Results with:
Which I find a little bit weird, as I would assume that range would be faster.