Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

random number function not working as advertised

0 views
Skip to first unread message

Alan

unread,
Oct 15, 2009, 6:33:29 PM10/15/09
to
I wanted to use "random" to generate random integer numbers between 1
and 13. At first I used "(1+ (random 12)" but I was not getting the
full range of numbers up to 13. Eventually I evaluated the following
in the scratch buffer

(let (aw-result aw-cnt)
(setq aw-cnt 130)
(while (> aw-cnt 0)
(setq aw-result (1+ (random 26)))
(setq aw-cnt (1- aw-cnt))
(princ (format "#=%s, " aw-result))))

and got as a result

#=13, #=12, #=3, #=7, #=6, #=11, #=6, #=10, #=2, #=12, #=5, #=2, #=3,
#=12, #=5, #=5, #=4, #=3, #=1, #=13, #=6, #=3, #=7, #=4, #=4, #=11,
#=9, #=1, #=4, #=12, #=10, #=12, #=2, #=1, #=5, #=7, #=2, #=1, #=12,
#=10, #=2, #=9, #=11, #=4, #=12, #=13, #=3, #=3, #=3, #=13, #=10, #=9,
#=12, #=11, #=2, #=12, #=12, #=13, #=5, #=12, #=6, #=4, #=7, #=11,
#=7, #=8, #=5, #=12, #=5, #=3, #=1, #=4, #=2, #=8, #=3, #=10, #=9,
#=3, #=13, #=3, #=1, #=1, #=2, #=8, #=3, #=4, #=6, #=4, #=7, #=13,
#=2, #=10, #=4, #=7, #=7, #=11, #=2, #=13, #=7, #=12, #=5, #=9, #=7,
#=4, #=11, #=12, #=7, #=6, #=5, #=8, #=11, #=1, #=1, #=4, #=10, #=2,
#=12, #=3, #=5, #=8, #=13, #=6, #=4, #=8, #=1, #=12, #=3, #=13, #=6,
#=5, nil

Comparing some frequencies:

13 appears 10 times
12 appears 16 times
11 appears 8 times
10 appears 7 times
1 appears 10 times
2 appears 11 times

XEmacs 21.4 (patch 21) "Educational Television" [Lucid] (i586-pc-
win32) of Sun Oct 07 2007 on VSHELTON-PC2

I tried the same thing with Emacs, with a result that indicates that
"random" there works as advertised. I show this below:

(let (aw-result aw-cnt)
(setq aw-cnt 130)
(while (> aw-cnt 0)
(setq aw-result (1+ (random 26)))
(setq aw-cnt (1- aw-cnt))
(princ (format "#=%s, " aw-result))))

#=1, #=11, #=5, #=19, #=17, #=10, #=11, #=16, #=5, #=8, #=26, #=7,
#=1, #=2, #=6, #=9, #=24, #=3, #=15, #=2, #=20, #=24, #=3, #=25, #=19,
#=15, #=26, #=6, #=14, #=18, #=20, #=10, #=1, #=15, #=5, #=18, #=26,
#=13, #=24, #=13, #=6, #=13, #=4, #=8, #=23, #=1, #=14, #=9, #=26,
#=11, #=8, #=8, #=11, #=23, #=25, #=8, #=17, #=10, #=17, #=9, #=13,
#=15, #=10, #=12, #=7, #=18, #=1, #=23, #=24, #=3, #=25, #=19, #=24,
#=26, #=13, #=11, #=18, #=1, #=2, #=4, #=2, #=18, #=9, #=18, #=6,
#=17, #=15, #=8, #=18, #=6, #=14, #=1, #=16, #=11, #=3, #=6, #=14,
#=3, #=23, #=11, #=23, #=5, #=7, #=1, #=11, #=10, #=10, #=24, #=6,
#=5, #=23, #=21, #=12, #=22, #=7, #=24, #=6, #=19, #=7, #=4, #=10,
#=3, #=13, #=6, #=5, #=26, #=13, #=7, #=16, #=4, nil

26 occurs 6 times
25 occurs 3 times
24 occurs 7 times

GNU Emacs 22.1.1 (i386-mingw-nt5.1.2600) of 2007-06-02 on RELEASE

I looked at the release notes for XEmacs 21.4.22 (the latest stable
version) and don't see anything there that would lead me to believe
that "random" in that release would do anything different.

Vassil Nikolov

unread,
Oct 16, 2009, 3:15:19 AM10/16/09
to

Using

(defun random-frequencies (n m)
"Generate N random numbers between 0 and M and produce their frequencies."
(random t)
(let ((result (make-vector m 0)))
(dotimes (i n result)
(incf (aref result (random m))))))

for a simple test,

(random-frequencies 10000 26)

returned:

XEmacs: [385 381 413 405 386 374 365 380 387 403 353 392 399 371 398 337 371 394 405 389 354 423 385 407 380 363]
GNU Emacs: [404 398 363 402 386 374 386 382 365 368 374 373 388 393 380 361 397 403 375 378 376 399 425 393 396 361]

which don't look significantly different (at least to the naked eye).

---Vassil.


--
"Even when the muse is posting on Usenet, Alexander Sergeevich?"

Alan

unread,
Oct 16, 2009, 11:36:38 AM10/16/09
to
When I repeat your test on

XEmacs 21.4 (patch 21) "Educational Television" [Lucid] (i586-pc-
win32) of Sun Oct 07 2007 on VSHELTON-PC2

I get

[747 776 730 745 752 847 780 752 770 783 744 773 801 0 0 0 0 0 0 0 0 0
0 0 0 0]

On

GNU Emacs 22.1.1 (i386-mingw-nt5.1.2600) of 2007-06-02 on RELEASE

I get

[378 381 392 377 402 396 384 413 357 382 393 388 ...]

which I suppose means that it isn't willing to show me the whole
vector. If I add a line (print result) then I get

[373 367 413 391 349 399 394 380 364 363 388 397 387 383 408 366 373
404 406 375 385 368 366 431 376 394]

Vassil Nikolov

unread,
Oct 16, 2009, 10:05:50 PM10/16/09
to

On Fri, 16 Oct 2009 08:36:38 -0700 (PDT), Alan <weh...@fnal.gov> said:

> When I repeat your test on

> XEmacs 21.4 (patch 21) "Educational Television" [Lucid] (i586-pc-win32) of Sun Oct 07 2007 on VSHELTON-PC2


> I get
> [747 776 730 745 752 847 780 752 770 783 744 773 801 0 0 0 0 0 0 0 0 0 0 0 0 0]

That is not right, of course; probably worth reporting to the
developers. I believe there is a newer 21.4 version; I don't know
how any of these versions' `random' behaves on Windows. (I ran my
example on 21.4.20 on x86_64-linux-gnu.)

> On
> GNU Emacs 22.1.1 (i386-mingw-nt5.1.2600) of 2007-06-02 on RELEASE
> I get
> [378 381 392 377 402 396 384 413 357 382 393 388 ...]
> which I suppose means that it isn't willing to show me the whole
> vector.

Per `eval-expression-print-length' (whose default value is 12).

0 new messages