0

I'm having trouble getting a number from Random.org. I am following their API, and I believe I have constructed the request correctly. Here is the request code:

self.GetResults = function () {
            $.ajax({
                url: 'https://api.random.org/json-rpc/1/invoke',
                dataType: "json",
                contentType: "application/json-rpc; charset=utf-8",
                type: 'POST',
                data: {
                    "jsonrpc": "2.0",
                    "method": "generateIntegers",
                    "params": {
                        "apiKey": "Get-a-free-one-at-the-site",
                        "n": 1,
                        "min": 1,
                        "max": 20
                    },
                    "id": 1
                }

            })
            .done(function (data, status, request)
            {
                alert("Success! " + data[0]);
            })
            .fail(function (request, status, error)
            {
                alert("Failed " + error);
            });
        }

The problems I am having are as follows:

  1. If I don't include contentType: "application/json; charset=utf-8" then the default type is "application/x-www-form-urlencoded; charset=UTF-8", per the jQuery API documentation. Random.org states in their API that they require a JSON request (application/json-rpc specifically, but application/json is acceptable). I therefore get a parse error in response.
  2. If I do include the content type mentioned above, I get the error, "Request header field Content-Type is not allowed by Access-Control-Allow-Headers."

I have tried to add code to the web.config to allow the request header as follows:

<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />
  </customHeaders>
</httpProtocol>

From this post https://stackoverflow.com/a/12413863/981202, but have had no luck.

I've also tried modifying the Global.asax as follows:

protected void Application_PreSendRequestHeaders(object sender, EventArgs e)
{
    HttpContext.Current.Response.Headers.Remove("Access-Control-Allow-Headers");
}

As in another Stack Overflow post (I don't have the link), but that didn't seem to do it either. For clarity, I put this function after the Application_Start() function.

You may be wondering why I don't just use the Random class. The reason is because I'd like to use Random.org if nothing else then as an exercise. I'd like to find a solution to this problem if one exists. Previously, I had some data here that indicated that the C# Random class didn't do a very good job, but I was corrected, so that data has been removed. Yes, the Random class is an option, though as I mentioned, I'd like to find a solution to this problem.

Community
  • 1
  • 1
admiral142
  • 105
  • 1
  • 2
  • 10
  • Can you link to the documentation you read on random.org? I'm looking at http://www.random.org/clients/http/, and based on what I'm seeing there, all you need to do is request the following url: http://www.random.org/integers/?num=1&min=1&max=20&col=1&base=10&format=plain&rnd=new. Also, even though System.Random isn't truly random, it seems that out of tens of thousands of trials, you'd see a nicer distribution than that. – pmcoltrane Apr 08 '14 at 19:34
  • can you show the where you implemented the C# random class? Running my own tests generates a much more straight line than yours, using the same data. – Jonesopolis Apr 08 '14 at 19:45
  • @ pmcoltrane: The Random.org documentation I'm reading is here: https://api.random.org/json-rpc/1/ Specifically here: https://api.random.org/json-rpc/1/introduction and here (under generateIntegers: https://api.random.org/json-rpc/1/basic – admiral142 Apr 08 '14 at 19:49
  • @Jonesy: I'll do my best to show you. The Counter class is simply a switch statement that increments a number by 1 each time it's generated. if (response == 'y') { while (DateTime.Now < end) { Random rand = new Random(); int randomInt = rand.Next(1, 21); counter.Count(randomInt); } } So you can see how I'm getting the random numbers here, which I hope is what you were looking for. :) – admiral142 Apr 08 '14 at 19:56
  • 1
    You should instantiate Random one time, outside of the loop. `Random rand = new Random();` creates a new instance of Random using the system timer as its seed value. By calling it inside of a loop, you're probably seeding it with the same timer value repeatedly. See the remarks for http://msdn.microsoft.com/en-us/library/h343ddh9.aspx. (I'll have to look over the json-rpc docs that you linked.) – pmcoltrane Apr 08 '14 at 20:01
  • @pmcoltrane: Thanks! I wrote the program I got the data from quite some time ago. I made the change you suggested and re-ran it just now, and I get a much better distribution. I'd still like to use Random.org, and if I can't, I guess the Random class is a much more viable option than I had previously thought. – admiral142 Apr 08 '14 at 20:11

1 Answers1

0
<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />
  </customHeaders>
</httpProtocol>

I have added this code to my web.config. At first I put it in the <configuration> node and it did not work. Then I realized that it should be within the <system.webServer> which is also within the <configuration> node. Once I put it there it worked and I did not receive the error mentioned again.

crisvass
  • 1
  • 3