[CoLoCo] [clue-talk] Re: Random Number Generators

Kevin Fries kfries at cctus.com
Thu Feb 21 18:47:53 GMT 2008


On Wed, 2008-02-20 at 22:12 -0700, Jim Hutchinson wrote:
> Leina,
> 
> In some discussions with others about using a computer to generate
> random numbers, it is clear that to do anything beyond generating one
> number at a time you will need some sort of small program or script.
> There may also be some programs that could be downloaded but using a
> script of your own would be more fun, I think. Programs and scripts
> are written in computer programming languages. There are many computer
> languages, but some of the more common ones (and ones you will be able
> to easily use on your Linux computer) are C or C++, Python and Ruby.
> I'm sure other people will have their own favorites as well. You may
> also be able to find something on the internet that is similar to what
> you want. In my discussions I also collected a couple examples. They
> are attached as text files. They will be run from a terminal
> (applications -> accessories -> terminal). For the Python script named
> dice.py you would change to the directory containing the file and then
> type
> 
Leina,

Ruby and Python are what are called object oriented languages.  What
that means is that instead of writing code to tell the computer what to
do, you write code to emulate objects or things.

In your case, there are three objects:
  A single die
  A group of die to be rolled at the same time, called dice
  A test where you roll the dice a number of times.

So you could get a getter indication of the way computers are actually
programmed, I wrote you a better version of the ruby program.

At the bottom of this code, you will see two ways of defining your test.

Each Die has a number of sides, a minimum value, and a value each number
is incremented by.  Unless told otherwise, this program assumes you are
throwing a standard six sided die, with one as is lowest value, and each
side increasing by 1 (i.e. 1, 2, 3, 4, 5, 6).  To show you how to define
your own die, the second test uses a ten sided die with values from zero
through nine.

Once you defined the die, you can define the set of dice.  Basically,
this simple example assumes that all the dice in the set are alike.  By
default, the system assumes that there are two standard die.  In my
second test, I increased the number to three, and use my custom die
instead of the standard ones (three, ten sided dice with values from
0-9).

Finally, you define the test.  A test consists of a set of dice, thrown
a number of times.  The default is to use a default set of dice (two,
six sided dice), 1000 times.  But again, you can decide.  So in the
second test, I threw my custom dice 500 times.

I hope this helps you get a better understanding of probability, as well
as a small taste of programming a computer to emulate real world
situations.  The coding I used was fairly simple, in hope that you will
be able to follow along.  Let me know if you have any questions

If you have ruby installed correctly, you should just be able to make
this file executable (chmod +x dice.rb), and run it like any other
script.

Good Luck

-- 
Kevin Fries
Senior Linux Engineer
Computer and Communications Technology, Inc
A Division of Japan Communications Inc.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: dice.rb
Type: application/x-ruby
Size: 1593 bytes
Desc: not available
Url : https://lists.ubuntu.com/archives/ubuntu-us-co/attachments/20080221/285e4bfe/attachment-0001.bin 


More information about the Ubuntu-us-co mailing list