fun(-)ction for drawing random numbers 1..6

View: New views
3 Messages — Rating Filter:   Alert me  

fun(-)ction for drawing random numbers 1..6

by Jaroslav Hajek-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

hi all,

if you ever needed to draw n random numbers from 1:6 in your script,
this function is for you.
OK, ceil (6*rand (1, n)) can do it as well, but this one is cooler.
Terminal with ansi-escapes required.

enjoy

--
RNDr. Jaroslav Hajek
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz

[rolldices.m]

## Copyright (C) 2009 Jaroslav Hajek <highegg@...>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; see the file COPYING.  If not, see
## <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn{Function File} rolldices (n)
## @deftypefnx{Function File} rolldices (n, nrep, delay)
## Returns n random numbers from the 1:6 range, displaying a visual selection
## effect. nrep sets the number of rolls, delay specifies time between successive
## rolls in seconds. Default is nrep = 25 and delay = 0.1.
## Requires a terminal with ANSI escape sequences enabled.
## @end deftypefn

function numbers = rolldices (n, nrep = 25, delay = .1)
  if (nargin != 1)
    print_usage ();
  endif

  persistent matrices = getmatrices ();

  screen_cols = getenv ("COLUMNS");
  if (isempty (screen_cols))
    screen_cols = 80;
  else
    screen_cols = str2num (screen_cols);
  endif

  dices_per_row = floor ((screen_cols-1) / 7);

  oldpso = page_screen_output (0);

  numbers = [];

  unwind_protect
    while (n > 0)
      m = min (n, dices_per_row);
      for i = 1:nrep
        if (i > 1)
          puts (char ([27, 91, 51, 70]));
          sleep (delay);
        endif
        nums = ceil (6 * rand (1, m));
        disp (matrices(:,:,nums)(:,:));
      endfor
      numbers = [numbers, nums];
      n -= m;
      puts ("\n");
    endwhile
  unwind_protect_cleanup
    page_screen_output (oldpso);
  end_unwind_protect

endfunction

function matrices = getmatrices ()
  lbrk = [27, 91, 55, 109](ones (1, 3), :);
  rbrk = [27, 91, 50, 55, 109](ones (1, 3), :);
  spcs = [32, 32; 32, 32; 32, 32];
  dchrs = reshape(
  ["     @    @    @   @@   @@   @";
   "  @         @         @  @   @";
   "         @    @@   @@   @@   @"], [3, 5, 6]);

  matrices = mat2cell (dchrs, 3, 5, ones (1, 6));
  matrices = cellfun (@(mat) [spcs, lbrk, mat, rbrk], matrices, "UniformOutput", false);
  matrices = cat (3, matrices{:});
endfunction


------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
Octave-dev mailing list
Octave-dev@...
https://lists.sourceforge.net/lists/listinfo/octave-dev

Re: fun(-)ction for drawing random numbers 1..6

by Søren Hauberg :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

lør, 26 09 2009 kl. 19:24 +0200, skrev Jaroslav Hajek:
> if you ever needed to draw n random numbers from 1:6 in your script,
> this function is for you.
> OK, ceil (6*rand (1, n)) can do it as well, but this one is cooler.
> Terminal with ansi-escapes required.

Ha ha! This is soo cool. It might be a bit slow for large scale Markov
Chain Monte Carlo studies, though :-)
But I can easily imagine using this when doing demos during lectures.

Yet another function that could go into the non-existing 'fun' package.

Søren


------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
Octave-dev mailing list
Octave-dev@...
https://lists.sourceforge.net/lists/listinfo/octave-dev

Re: fun(-)ction for drawing random numbers 1..6

by Daniel J Sebald :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Wasn't there a discussion recently about a "games" package in response to a script someone sent in?  This would make two such scripts, enough for a package.

Dan


Jaroslav Hajek wrote:

> hi all,
>
> if you ever needed to draw n random numbers from 1:6 in your script,
> this function is for you.
> OK, ceil (6*rand (1, n)) can do it as well, but this one is cooler.
> Terminal with ansi-escapes required.
>
> enjoy
>
>
>
> ------------------------------------------------------------------------
>
> ## Copyright (C) 2009 Jaroslav Hajek <highegg@...>
> ##
> ## This program is free software; you can redistribute it and/or modify
> ## it under the terms of the GNU General Public License as published by
> ## the Free Software Foundation; either version 3 of the License, or
> ## (at your option) any later version.
> ##
> ## This program is distributed in the hope that it will be useful,
> ## but WITHOUT ANY WARRANTY; without even the implied warranty of
> ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> ## GNU General Public License for more details.
> ##
> ## You should have received a copy of the GNU General Public License
> ## along with this program; see the file COPYING.  If not, see
> ## <http://www.gnu.org/licenses/>.
>
> ## -*- texinfo -*-
> ## @deftypefn{Function File} rolldices (n)
> ## @deftypefnx{Function File} rolldices (n, nrep, delay)
> ## Returns n random numbers from the 1:6 range, displaying a visual selection
> ## effect. nrep sets the number of rolls, delay specifies time between successive
> ## rolls in seconds. Default is nrep = 25 and delay = 0.1.
> ## Requires a terminal with ANSI escape sequences enabled.
> ## @end deftypefn
>
> function numbers = rolldices (n, nrep = 25, delay = .1)
>   if (nargin != 1)
>     print_usage ();
>   endif
>
>   persistent matrices = getmatrices ();
>
>   screen_cols = getenv ("COLUMNS");
>   if (isempty (screen_cols))
>     screen_cols = 80;
>   else
>     screen_cols = str2num (screen_cols);
>   endif
>
>   dices_per_row = floor ((screen_cols-1) / 7);
>
>   oldpso = page_screen_output (0);
>
>   numbers = [];
>
>   unwind_protect
>     while (n > 0)
>       m = min (n, dices_per_row);
>       for i = 1:nrep
>         if (i > 1)
>           puts (char ([27, 91, 51, 70]));
>           sleep (delay);
>         endif
>         nums = ceil (6 * rand (1, m));
>         disp (matrices(:,:,nums)(:,:));
>       endfor
>       numbers = [numbers, nums];
>       n -= m;
>       puts ("\n");
>     endwhile
>   unwind_protect_cleanup
>     page_screen_output (oldpso);
>   end_unwind_protect
>
> endfunction
>
> function matrices = getmatrices ()
>   lbrk = [27, 91, 55, 109](ones (1, 3), :);
>   rbrk = [27, 91, 50, 55, 109](ones (1, 3), :);
>   spcs = [32, 32; 32, 32; 32, 32];
>   dchrs = reshape(
>   ["     @    @    @   @@   @@   @";
>    "  @         @         @  @   @";
>    "         @    @@   @@   @@   @"], [3, 5, 6]);
>
>   matrices = mat2cell (dchrs, 3, 5, ones (1, 6));
>   matrices = cellfun (@(mat) [spcs, lbrk, mat, rbrk], matrices, "UniformOutput", false);
>   matrices = cat (3, matrices{:});
> endfunction
>
>
> ------------------------------------------------------------------------
>
> ------------------------------------------------------------------------------
> Come build with us! The BlackBerry® Developer Conference in SF, CA
> is the only developer event you need to attend this year. Jumpstart your
> developing skills, take BlackBerry mobile applications to market and stay
> ahead of the curve. Join us from November 9-12, 2009. Register now!
> http://p.sf.net/sfu/devconf
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Octave-dev mailing list
> Octave-dev@...
> https://lists.sourceforge.net/lists/listinfo/octave-dev


--

Dan Sebald
email: daniel(DOT)sebald(AT)ieee(DOT)org
URL: http://www(DOT)dansebald(DOT)com

------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
Octave-dev mailing list
Octave-dev@...
https://lists.sourceforge.net/lists/listinfo/octave-dev