Friday, September 17, 2010

simple 16-bit random number generator

simple 16-bit random number generator

Do you have a favorite?

I need small numbers randomized each iteration within a function. The
following seems to work well for application.

Converted the forth code:

\ 16-bit random number generator, Leo Brodie 'Starting Forth'
VARIABLE RND HERE RND ! ( seed )
: RANDOM ( -- u ) RND @ 31421 * 6927 + DUP RND ! ;
: CHOOSE ( u -- u ) RANDOM UM* SWAP DROP ; \ 0 u within

To a vim script:

let rnd = localtime() % 0x10000

function! Random()
let g:rnd = (g:rnd * 31421 + 6927) % 0x10000
return g:rnd
endfun

function! Choose(n) " 0 n within
return (Random() * a:n) / 0x10000
endfun

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments:

Post a Comment