Results 1 to 4 of 4

Thread: Getting numbers from Random.org
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    New Member
    Join Date
    Jun 2019
    Posts
    4
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Getting numbers from Random.org

    I am trying to use random.org to get a few random numbers (I am not super advanced in writing in VBA so I am just going to get them one at a time). I saw sample code that was written in a reply to a post in 2012 on this site. I tried using the code but I got an error Run-time error '429' ActiveX component can't create object. I admittedly know nothing about objects but was just copying the code that I saw. Can someone please let me know how I can get this to work? I have not finished the code but was just testing that I'd be able to send out the request and get a number back and it failed. I can tell you that if I copy and paste the URL I have below, it does give me one random number between 1 and 6 on random.org's webpage. So I know that isn't the problem at least. What I have so far is:

    Code:
    Sub BattingRoll()
    
    
        Dim dsXMLObj As Object
        Dim dsURL As String
        Dim dsRespText
        Dim I As Long
        Dim dsBattingCol As Long
        Dim dsBattingTot As Long
    
    
        dsURL = "http://www.random.org/integers/?num=1&min=1&max=6&col=1&base=10&format=plain&rnd=plain"
        Set dsXMLObj = CreateObject("MSXML2.ServerXMLHTTP.4.0")
        With dsXMLObj
            .Open "GET", dsURL, False
            .send
            
            dsRespText = .responseText
            dsBattingCol = Val(Split(dsRespText, Chr(10)))
        End With
        
    End Sub
    The original code posted by the person who replied in 2012 was as follows. I'm not sure if objects or anything way beyond my knowledge level has changed or if I just messed something up.

    Code:
    Option Explicit
    Function GetRndNos(NUM As Long, MIN As Long, MAX As Long) As Variant
    Dim objXMLHTTP As Object
    Dim strURL As String
    Dim strResp
    Dim I As Long
        strURL = "http://www.random.org/integers/"
        strURL = strURL & "?num=" & NUM & "&min=" & MIN & "&max=" & MAX & "&col=1&base=10&format=plain&rnd=plain"
        Debug.Print strURL
        Set objXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP.4.0")
        
        With objXMLHTTP
            .Open "GET", strURL, False
            .send
            
            strResp = .responseText
            
            GetRndNos = Split(strResp, Chr(10))
            
        End With
        
        Set objXMLHTTP = Nothing
    End Function
    
    Sub test()
        Range("A1:A100").Value = Application.Transpose(GetRndNos(100, 1, 100))
    End Sub

  2. #2
    Board Regular Kyle123's Avatar
    Join Date
    Jan 2012
    Location
    Leeds, UK
    Posts
    2,642
    Post Thanks / Like
    Mentioned
    10 Post(s)
    Tagged
    2 Thread(s)

    Default Re: Getting numbers from Random.org

    You want this:
    Code:
    Sub BattingRoll()
    
    
        Dim dsXMLObj As Object
        Dim dsURL As String
        Dim dsRespText
        Dim I As Long
        Dim dsBattingCol As Long
        Dim dsBattingTot As Long
    
    
        dsURL = "https://www.random.org/integers/?num=1&min=1&max=6&col=1&base=10&format=plain&rnd=plain"
        Set dsXMLObj = CreateObject("MSXML2.XMLHTTP")
        With dsXMLObj
            .Open "GET", dsURL, False
            .send
            
            dsRespText = .responseText
            dsBattingCol = Val(dsRespText)
        End With
        
    End Sub

  3. #3
    New Member
    Join Date
    Jun 2019
    Posts
    4
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Getting numbers from Random.org

    Thank you for your help. It was able to get past that, but when it gets to ".send" I get another error, a Run-time error that says "Access is denied." Any thoughts? I really have no idea where to go. Thanks again.

  4. #4
    Board Regular Kyle123's Avatar
    Join Date
    Jan 2012
    Location
    Leeds, UK
    Posts
    2,642
    Post Thanks / Like
    Mentioned
    10 Post(s)
    Tagged
    2 Thread(s)

    Default Re: Getting numbers from Random.org

    Note the different url in my code compared to yours, specifically https

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •