RE: [PATCH] provide random number generating functions for vimscript

41 views
Skip to first unread message

xuh...@gmail.com

unread,
Nov 8, 2010, 8:50:58 PM11/8/10
to Bram Moolenaar, vim...@googlegroups.com
Bram:
I have reduced the size of the patch and fixed white space. The attachment is the new patch.

I don't know why we have to reset the seed to a known value.

It's impossible for a computer to generate continual random float number, we have to generate discrete values and convert them to appropriate float numbers.

Hong Xu
2010/11/9

-----Original Message-----
From: Bram Moolenaar
Sent: 2010/11/09 02:08:27
To: H Xu
Cc: vim...@googlegroups.com
Subject: Re: [PATCH] provide random number generating functions for vimscript


Hong Xu wrote:

> The attachment is a patch which provides two pseudo-random number generating
> functions for vimscript.

Well, the patch is a bit big, I wonder if it can be done in a simpler
way.

At the same time, I think there should be a way to reset the seed to a
known value. Needed for testing at least.

Generating the float random number from an int looks strange.
Is that really needed? The computations look like we are dropping some
bits.

For code style: please fix white space, match the rest of the file.

--
"When I die, I want a tombstone that says "GAME OVER" - Ton Richters

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ download, build and distribute -- http://www.A-A-P.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

rand.patch

Ben Fritz

unread,
Nov 9, 2010, 9:16:34 AM11/9/10
to vim_dev


On Nov 8, 7:50 pm, "xuh...@gmail.com" <xuh...@gmail.com> wrote:
>
> I don't know why we have to reset the seed to a known value.
>

It is nice, especially for testing purposes, to be able to always have
the SAME sequence of "random" numbers. For example, if you write a
test case that runs a piece of code with random inputs 1,000,000
times, and you see a failure, it would be nice to figure out what
inputs caused the failure or at least to be able to re-run the test to
make sure you actually fixed the error. Allowing the user to set the
seed value gives repeatability to such tests.

Or, comparing two algorithms may be fairer if you run them on the SAME
sequence of "random" data.

I'm sure there are other use cases too where this would be useful.

H Xu

unread,
Nov 9, 2010, 11:24:23 AM11/9/10
to Bram Moolenaar, vim...@googlegroups.com
OK, the attachment is the latest patch which I add a srand function to set seed.
rand.patch

Bram Moolenaar

unread,
Nov 10, 2010, 6:45:52 AM11/10/10
to xuh...@gmail.com, vim...@googlegroups.com

Hong Xu wrote:

> I have reduced the size of the patch and fixed white space. The
> attachment is the new patch.

Thanks. I'll have another look later.

> I don't know why we have to reset the seed to a known value.

Sometimes it's useful to reproduce the sequence of numbers. Mainly for
debugging and testing.

> It's impossible for a computer to generate continual random float
> number, we have to generate discrete values and convert them to
> appropriate float numbers.

Then why don't we drop the method to generate a float random number and
only provide the integer one?

If we do need it, another simplification would be to have the float
random number always be between 0.0 and 1.0. The use of a range is much
less useful than for integers.

I know this kind of functionality will be used very infrequently, we
need to keep it as simple and small as possible.

--
ARTHUR: No, hang on! Just answer the five questions ...
GALAHAD: Three questions ...
ARTHUR: Three questions ... And we shall watch ... and pray.
"Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD

H Xu

unread,
Nov 10, 2010, 11:03:34 AM11/10/10
to Bram Moolenaar, vim...@googlegroups.com
To Bram:

Yes, randfloat would not be frequently used.

The attachment is a new patch which simplifies randfloat function and makes srand function more convenient to use.

Hong Xu
2010/11/10

-----Original Message-----
From: Bram Moolenaar

rand.patch
Reply all
Reply to author
Forward
0 new messages