Random number issue?

36 views
Skip to first unread message

Ryan Gonzalez

unread,
Feb 24, 2015, 8:50:52 PM2/24/15
to kona...@googlegroups.com
More code golf. Anyway, I'm trying to solve this golf. Now, I've written this:

pi:4*_atan 1
f:{\r x;a:_sqrt[-2*_log y?1.];b:_cos[2*pi*y?1.];a*b}

It's not very concise, but I'll work on that after I get it working.

It's a port of the R code:

f=function(S,N){set.seed(S);sqrt(-2*log(runif(N)))*cos(2*pi*runif(N))}

However, it's supposed to return the same value when called twice with the same numbers, but it doesn't:

ryan@DevPC-LX:~/golf/ndist$ k ndist.k
K Console - Enter \ for help

  3.141593
{\r x;a:_sqrt[-2*_log y?1.];b:_cos[2*pi*y?1.];a*b}
2
0.6852126 -0.7662578 -0.8555918 1.444211 -0.9106986
2
0.1604498 -0.6533239 -1.091418 -0.6507569 0.04060063
    ryan@DevPC-LX:~/golf/ndist$ k ndist.k
K Console - Enter \ for help

  3.141593
{\r x;a:_sqrt[-2*_log y?1.];b:_cos[2*pi*y?1.];a*b}
2
1.568658 -0.3935577 -0.4553107 -0.451638 0.41696
2
0.486304 -1.552315 -1.206765 -1.408645 0.9728425
    ryan@DevPC-LX:~/golf/ndist$ 

What am I doing wrong?

Also, I'm sorry that I keep asking questions here, but this is the only way I know of to figure K out.

--
Ryan
If anybody ever asks me why I prefer C++ to C, my answer will be simple: "It's becauseslejfp23(@#Q*(E*EIdc-SEGFAULT. Wait, I don't think that was nul-terminated."
Personal reality distortion fields are immune to contradictory evidence. - srean
Check out my website: http://kirbyfan64.github.io/

Tom Szczesny

unread,
Feb 25, 2015, 12:03:19 AM2/25/15
to kona...@googlegroups.com
Please display the complete contents of ndist.k
so it is clear what values you are using as arguments to f.

--
You received this message because you are subscribed to the Google Groups "Kona Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kona-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kevin Lawler

unread,
Feb 25, 2015, 10:46:19 AM2/25/15
to kona...@googlegroups.com
Fix the seed to a constant using "\r". To do this by default, add the
command to the startup script. Kona uses a randomized seed by default
as that is the more sensible to default in this domain.

Ryan Gonzalez

unread,
Feb 25, 2015, 1:03:26 PM2/25/15
to kona...@googlegroups.com
That's what I'm doing! Here's the complete file:

pi:4*_atan 1
/ x = seed
/ y = # of numbers
f:{\r x;a:_sqrt[-2*_log y?1.];b:_cos[2*pi*y?1.];a*b}
f[2;5]
f[2;5]

I put the \r call in the beginning of f.

Kevin Lawler

unread,
Feb 25, 2015, 1:52:18 PM2/25/15
to kona...@googlegroups.com
What happens if you put it outside the function and with a literal
number instead of a variable?

Ryan Gonzalez

unread,
Feb 25, 2015, 2:16:13 PM2/25/15
to kona...@googlegroups.com
If I put it outside, it works. However, the golf competition requires that it be a) a function or b) a complete program, and I can't figure out how to get K to read from stdin.

Kevin Lawler

unread,
Feb 25, 2015, 2:18:27 PM2/25/15
to kona...@googlegroups.com
Read from stdin is 0:`

Ryan Gonzalez

unread,
Feb 25, 2015, 2:31:04 PM2/25/15
to kona...@googlegroups.com
Ok...

Two more issues:

1. I can't find any way to go from a string to an int, other than _ic, which gets the ASCII value of a character; not what I want
2. I STILL need some way to set the random seed to a variable. Is this impossible?

Kevin Lawler

unread,
Feb 25, 2015, 2:48:40 PM2/25/15
to kona...@googlegroups.com
1.

0$"123"

123

0.0$"123.5"

123.5

2. I think the variable works if you do it outside the function.

Ryan Gonzalez

unread,
Feb 25, 2015, 3:04:33 PM2/25/15
to kona...@googlegroups.com
ryan@DevPC-LX:~$ k
K Console - Enter \ for help

  v:2
2
  v
2
  \r v
type error
>  

I can confirm that editing the script gives the same error.

Kevin Lawler

unread,
Feb 26, 2015, 2:11:52 PM2/26/15
to kona...@googlegroups.com
So it seems like this functionality doesn't exist yet. Backslash
commands tend to be more for console use and less for function use.
Nevertheless, it's probably easy to modify the random seed command to
work like the timing command. What you're doing with the random number
generator is a bit artificial. It is fine however for anyone to update
the command with this functionality.

Kevin Lawler

unread,
Mar 20, 2015, 1:58:16 PM3/20/15
to kona...@googlegroups.com
Another discussion reminds me of an alternate solution: you could generate the string to do this, something like ("\\r ", $v) and then execute it using '.' or "dot", which is eval. This should work inside of the function.

Ryan Gonzalez

unread,
Mar 20, 2015, 2:14:03 PM3/20/15
to kona...@googlegroups.com
Doesn't seem to work:

K Console - Enter \ for help

  v:2
2
  . ("\\r ", $v)
2
2
  \r
954325357106671504

Ryan
[ERROR]: Your autotools build scripts are 200 lines longer than your program. Something’s wrong.

Bakul Shah

unread,
Mar 20, 2015, 5:34:00 PM3/20/15
to kona...@googlegroups.com
On Fri, 20 Mar 2015 13:13:42 CDT Ryan Gonzalez <rym...@gmail.com> wrote:
> Doesn't seem to work:

In k, the random number seed always starts out at -314159 and
multiple uses of _draw are completely predictable. But any
time you reference \r, it is reset to to -314159.

\r
-314159
10 _draw 10
9 4 4 7 9 9 4 7 8 9 <== A
10 _draw 10
7 9 4 7 8 8 3 9 9 4 <== B
\r
-314159
10 _draw 10
9 4 4 7 9 9 4 7 8 9 <== A
10 _draw 10
7 9 4 7 8 8 3 9 9 4 <== B

You can also set \r to anything you want. Referencing \r
resets it to the value you had set (so again predictable
behavior):

\r 1
10 _draw 10
5 6 2 6 3 2 8 8 2 1 <== C
\r
1
10 _draw 10
5 6 2 6 3 2 8 8 2 1 <== C
\r -314159
10 _draw 10
9 4 4 7 9 9 4 7 8 9 <== A

In contrast kona starts with an arbitray random seed on every
kona invocation.

$ kona
\r
-2310217006860213009
$ kona
\r
-4453049770588971547

And the repeatability as in k doesn't exist:

\r
1827764524537755691
10 _draw 10
8 9 5 7 2 5 3 6 9 4
10 _draw 10
9 9 6 7 3 2 2 3 8 7
10 _draw 10
0 1 7 4 6 4 2 6 8 5
\r
1827764524537755691
10 _draw 10
8 8 0 2 9 4 0 1 0 6
10 _draw 10
0 6 2 0 5 5 7 4 5 2
10 _draw 10
2 9 4 0 0 2 6 3 5 0

On balance I like what k does as any simulation you do will be
predictable. You can get the existing kona behavior by setting
\r to a random value but you can't get k behavior at all.

So I think kona should follow what k does here.
Reply all
Reply to author
Forward
0 new messages