Support Forums

Full Version: [Challenge] Random
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Here's another challenge. the challenge this time is to create a script/program (any language) that will randomize the contence of an array without using external libries. This must work by:
Code:
new_array = [1, 2, 3, 4, 5]
new_array.randomize! #This will randomize everything e.g new_array now  = [3, 1, 5, 4, 2]

Post you code and explain how it works.
Well you can't really be completely random, there needs to be some sort of pattern. This script can take me anywhere from 10 minutes to 2 hours, depends how "random" you want it to be.
Code:
class Array
  def randomize!
    self.replace(self.sort_by { rand})
  end
end

This sorts the array by using a random number generated by rand. Witch is in kernel modual.
This should be random enought. ;)
Wow so ruby has a random module built in? No fair!
(02-26-2010, 06:17 AM)uber1337 Wrote: [ -> ]Wow so ruby has a random module built in? No fair!

That sir, is why Ruby > Python. Smile
Lol yes because Ruby coders are too lazy to type
Code:
import random
xD. Seriously, I would be very suprised if Ruby had as many useful libraries as python.
Wikipedia Wrote:Python has a large standard library, commonly cited as one of Python's greatest strengths, providing pre-written tools suited to many tasks. This is deliberate and has been described as a "batteries included" Python philosophy. The modules of the standard library can be augmented with custom modules written in either C or Python. Recently, Boost C++ Libraries includes a library, Boost.Python, to enable interoperability between C++ and Python. Because of the wide variety of tools provided by the standard library, combined with the ability to use a lower-level language such as C and C++, which is already capable of interfacing between other libraries, Python can be a powerful glue language between languages and tools.

The standard library is particularly well tailored to writing Internet-facing applications, with a large number of standard formats and protocols (such as MIME and HTTP) already supported. Modules for creating graphical user interfaces, connecting to relational databases, arithmetic with arbitrary precision decimals, manipulating regular expressions, and doing unit testing are also included.

Some parts of the standard library are covered by specifications (for example, the WSGI implementation WSGI follows PEP 333), but the majority of the modules are not. They are specified by their code, internal documentation, and test suite (if supplied). However, because most of the standard library is cross-platform Python code, there are only a few modules that must be altered or completely rewritten by alternative implementations.
(02-26-2010, 06:50 AM)uber1337 Wrote: [ -> ]Lol yes because Ruby coders are too ]
xD. Seriously, I would be very suprised if Ruby had as many useful libraries as python.

Ruby doesn't ;_;
LOL, I could give you a python script.... but I don't think you'd want that.... lol.
(02-26-2010, 05:00 PM)Μαύρο Wrote: [ -> ]LOL, I could give you a python script.... but I don't think you'd want that.... lol.

Go for it Big Grin
Basically cheating... but who cares! :p

Code:
import random
while 1 > 0:
        def rprocess(a):
            array = list(a)
            rarray= str(random.choice(array))
            return(rarray)
        p = raw_input('>>> ')
        r = rprocess(p)
        print(r)
It's actually a little more than the contest required, but oh well. :p