Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6096861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:58:34+00:00 2026-05-23T12:58:34+00:00

I recently ran into an interesting but annoying F Sharp behavior. According to [1],

  • 0

I recently ran into an interesting but annoying F Sharp behavior. According to [1], “F# automatically caches the value of any function which takes no parameters.” This seems like a good idea, but it is causing problems for me as I try to come up with a wrapper function to generate random numbers.

As an example, I have two different functions in the code at the end of this question. The first function “getRand” takes no parameters, but unfortunately it always returns the same number. The second function “getRand2” works as I would expect generating a new random number each time it is called, but it annoyingly takes a useless and ignored extra parameter.

If possible, I would like to have the functionality of getRand2 but the convenience of getRand. Is there a compiler directive or special keyword I can apply to getRand that will turn off its function caching capabilities and thereby cause it to behave like getRand2?

With Thanks,

Shawn
Note: Forgive me if the answer already appears in [1], I’m just not seeing it right now.
[1] – http://en.wikibooks.org/wiki/F_Sharp_Programming/Caching

(* Always returns the same number *)
let getRand = 
   let seed = int32(System.DateTime.Now.Ticks)   
   let randGen = new System.Random(seed)
   randGen.Next()

(* Works as expected except I need an annoying extra parameter *)
let getRand2 dummyParam = 
   let seed = int32(System.DateTime.Now.Ticks)   
   let randGen = new System.Random(seed)
   randGen.Next()

(* Outputs three "identical" numbers to console *)
System.Console.WriteLine(
   "Parameterless getRand always outputs same number.")
System.Console.WriteLine(getRand)
System.Threading.Thread.Sleep(100)
System.Console.WriteLine(getRand)
System.Threading.Thread.Sleep(100)
System.Console.WriteLine(getRand)
System.Console.WriteLine()

(* Outputs three "different" numbers to console *)
System.Console.WriteLine(
   "GetRand2 works as expected even though second dummy param is always the same.")
System.Console.WriteLine(getRand2 0)
System.Threading.Thread.Sleep(100)
System.Console.WriteLine(getRand2 0)
System.Threading.Thread.Sleep(100)
System.Console.WriteLine(getRand2 0)
System.Console.WriteLine()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:58:35+00:00Added an answer on May 23, 2026 at 12:58 pm

    Just to clarify a bit, I think that the phrase “function which takes no parameters” is misleading. By definition, a function maps a value from the function’s domain to a value in the function’s range, so all functions take parameters. In your case, getRand is not bound to a function, it’s just a value of type int.

    If I understand your question correctly, I think you want to do

    let getRand =    
        let seed = int System.DateTime.Now.Ticks      
        let randGen = new System.Random(seed)   
        fun () -> randGen.Next()
    

    You still need to call getRand as a function (getRand(), not just getRand), but there’s no way to work around this – the fact that an int value always remains the same is a critical feature for reasoning about a program.

    You could use your getRand2 function in much the same way as my version of getRand: since you don’t use dummyParam within the body, F# makes the function generic, which means that you can pass the unit value () as the argument if you want. However, your getRand2 function is broken, in that it creates a new random number generator each time it is called. This means that if you call it twice within one tick, you will get the same answer:

    let x,y = (getRand2(), getRand2())
    

    That’s why it’s important to define seed and randGen outside of the scope of the anonymous function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently ran into an issue at work in which, at least according to
I ran into an interesting behavior recently. It seems that if I override .equals()
Interesting problem I ran into recently: I implemented a Stream class (a wrapping stream
I recently ran into a situation in which accessing session information from view is
I recently ran into gwan ( http://gwan.ch ) a free web server that according
I've recently ran into some code that checks Page.IsCallBack but I wasn't sure how
I recently ran into a very sneaky bug, in which I forget to dereference
I've recently ran into a very interesting problem while writing a web app with
I recently ran into a pretty unexpected (to me) behavior of JPA (provided by
I recently ran into a bug in my code when using boost::bind . From

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.