caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Nathan Mishra Linger" <nathan.mishralinger@gmail.com>
To: "Ollie Frolovs" <ollie.frolovs.2012@my.bristol.ac.uk>
Cc: "caml users" <caml-list@inria.fr>
Subject: Re: [Caml-list] Random.self_init in Jane Street Core
Date: Thu, 22 May 2014 17:12:55 -0700 (PDT)	[thread overview]
Message-ID: <1400803974381.6f199022@Nodemailer> (raw)
In-Reply-To: <762DAB52-9D2E-4002-80B0-526258DD7856@my.bristol.ac.uk>

[-- Attachment #1: Type: text/plain, Size: 4265 bytes --]

I think the problem is that you aren't *calling* Random.self_init. To do so, change the line


  let _ = Random.self_init




to




  let () = Random.self_init ()




The value that _ is matching against in your program has type unit -> unit rather than just unit. 
—
Sent from Mailbox

On Wed, May 21, 2014 at 8:12 AM, Ollie Frolovs
<ollie.frolovs.2012@my.bristol.ac.uk> wrote:

> Hello
> I’ve been trying to use Random.self_init in Jane Street’s Core but every time I run my program it returns the same result as if the self_init is in fact completely deterministic. Two questions – what am I doing wrong and how do I make it “random” (as in returning different values on each run of the application).
> I compile the following source into native code with “corebuild”.
> When I run the program, the result is ALWAYS
> 1 0                                                                            
> 1 1                                                                             
> 0 1
> 0 2
> 0 3
> I’ve also upload the code and the output from "opam list -i” on GitHub, if that’s more convenient https://gist.github.com/olliefr/d6312d8195e9a30aa80c
> I believe I have the latest compiler/libraries. The system is OS X Mavericks.
> Many thanks,
> Ollie
> --
> (* SOURCE CODE BEGINS *) 
> open Core.Std                                                                   
>                                                                                 
> let _ = Random.self_init                                                        
> (*                                                                              
>   FIXME there must be something in the standard library to do this!             
>                                                                                 
>   Iterate a function over a value, tail-recursively.                            
>   n: how many times                                                             
>   f: function to apply                                                          
>   a: initial value of the argument                                              
> *)                                                                              
> let rec iterate n f a =                                                         
>   if n<=0                                                                       
>   then a                                                                        
>   else iterate (n-1) f (f a)                                                    
> (* Wandering Light *)                                                           
> let light = (0,0)
> let wander (x,y) =                                                              
>   match (1 + Random.int 4) with                                                 
>       1 -> (x+1, y)                                                             
>     | 2 -> (x, y+1)                                                             
>     | 3 -> (x-1, y)                                                             
>     | 4 -> (x, y+1)                                                             
>     | _ -> failwith "random direction is not 1 to 4, wtf?"                      
>                                                                                 
> let render (x,y) = printf "%i %i\n" x y                                         
>                                                                                 
> let step light =                                                                
>   let newlight = wander light in                                                
>   render newlight;                                                              
>   newlight                                                                      
>                                                                                 
> let _ = iterate 5 step (0,0)
> (* THE END *)
> -- 
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs

[-- Attachment #2: Type: text/html, Size: 4516 bytes --]

  parent reply	other threads:[~2014-05-23  0:12 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-21 12:10 Ollie Frolovs
2014-05-21 12:18 ` Dmitry Grebeniuk
2014-05-21 12:25   ` Ollie Frolovs
2014-05-21 12:28     ` David House
2014-05-21 16:40       ` Martin Jambon
2014-05-22  9:01         ` Ben Millwood
2014-05-22 17:22           ` Martin Jambon
2014-05-23  0:12 ` Nathan Mishra Linger [this message]
2014-05-23  0:15   ` Nathan Mishra Linger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1400803974381.6f199022@Nodemailer \
    --to=nathan.mishralinger@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=ollie.frolovs.2012@my.bristol.ac.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).