Random not so random?

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4

46 Replies - 2913 Views - Last Post: 16 June 2013 - 09:15 PM Rate Topic: ***** 1 Votes

#31 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1954
  • View blog
  • Posts: 4,055
  • Joined: 11-December 07

Re: Random not so random?

Posted 15 June 2013 - 07:39 AM

Sounds perfect then. :)

If you notice it getting slow, switch to a hashtable for the unique check.

This post has been edited by cfoley: 15 June 2013 - 07:40 AM

Was This Post Helpful? 0
  • +
  • -

#32 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1052
  • View blog
  • Posts: 4,077
  • Joined: 02-July 08

Re: Random not so random?

Posted 15 June 2013 - 08:07 AM

So hashtables have better performance than a List(Of T) - would not have thought that.
Was This Post Helpful? 0
  • +
  • -

#33 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: Random not so random?

Posted 15 June 2013 - 08:07 AM

Now that's something I didn't know but definitely worth knowing.
Was This Post Helpful? 0
  • +
  • -

#34 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 109
  • View blog
  • Posts: 603
  • Joined: 01-October 08

Re: Random not so random?

Posted 15 June 2013 - 08:08 AM

So something like this?

    Dim colnames As New Dictionary(Of String, String)
    'Dim prng As New Random
    'Const alphas As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

    Private Function RndColName() As String
        Dim sb As Guid
        Dim s As String
        'Dim ct As Integer = 0
        Do
            sb = Guid.NewGuid
            s = sb.ToString.Replace("-", "")
            's = alphas(prng.Next(alphas.Length)) & s & alphas(prng.Next(alphas.Length))
            'ct += 1
            'If ct > 3 Then
            '    'loop?
            '    Stop
            'End If
        Loop While colnames.ContainsKey(s)
        colnames.Add(s, "")
        Return s
    End Function


Was This Post Helpful? 0
  • +
  • -

#35 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1954
  • View blog
  • Posts: 4,055
  • Joined: 11-December 07

Re: Random not so random?

Posted 15 June 2013 - 08:45 AM

Yes, something like that. HashSet would be marginally better than the Dictionary or HashTable classes so you don't need values, just keys.

Quote

So hashtables have better performance than a List(Of T) - would not have thought that.


Yes, hashtables are designed for fast lookup. In short, the hashtable performs some calculation (the hash function) on the object to be stored. This calculation comes up with an integer that determines what array index the object gets stored at. To see if an object is in the hashtable, perform the same calculation and see if the object is in that array index. No matter how big the hashtable, if the table and hash function are designed well, the lookup will take a constant amount of time.

Using a list, the item you are looking for could be anywhere so you have to scan the items one by one. In this case especially, you are hoping that the item won't be there so most of the time you will scan the entire list. If the list grows large, you might notice the time it takes to scan.
Was This Post Helpful? 1
  • +
  • -

#36 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1052
  • View blog
  • Posts: 4,077
  • Joined: 02-July 08

Re: Random not so random?

Posted 15 June 2013 - 10:55 AM

@cfoley I don't see any performance problems with my usage(very small set of data). Now I know what to do if I need to - thanks for sharing that.
Was This Post Helpful? 0
  • +
  • -

#37 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 109
  • View blog
  • Posts: 603
  • Joined: 01-October 08

Re: Random not so random?

Posted 15 June 2013 - 11:19 AM

For small sets of data the list likely performs better.
Was This Post Helpful? 0
  • +
  • -

#38 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1954
  • View blog
  • Posts: 4,055
  • Joined: 11-December 07

Re: Random not so random?

Posted 15 June 2013 - 11:33 AM

Quote

@cfoley I don't see any performance problems with my usage(very small set of data). Now I know what to do if I need to - thanks for sharing that.


I'm not surprised. It's the kind of thing you would only notice if the lookup was in the middle of a large loop.

Quote

For small sets of data the list likely performs better.


Maybe. It depends how relatively expensive the hash function and equals() function are. These will change depending on what class of object is being stored.

This post has been edited by cfoley: 15 June 2013 - 11:34 AM

Was This Post Helpful? 0
  • +
  • -

#39 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7649
  • View blog
  • Posts: 12,905
  • Joined: 19-March 11

Re: Random not so random?

Posted 15 June 2013 - 02:19 PM

View Postdbasnett, on 15 June 2013 - 01:19 PM, said:

For small sets of data the list likely performs better.



Depends on the implementations, of course - again, I'm not a VB guy, so I'd have to check the details - but typically, no. A hash is basically a fast artithmetic process: it takes a large number and returns a number in a smaller space. This value is then used to index into a table. Hash tables can get slow, but usually that'll be because you're trying to hash into too small a space and you're getting collisions. Performance of a list depends on the implementation. An array-backed list can do fast lookup, a linked list can't. Don't know what VB uses, but the upshot is, your list probably doesn't perform much better than the hash table, if at all, and it might be significantly worse.
Was This Post Helpful? 0
  • +
  • -

#40 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3371
  • View blog
  • Posts: 11,420
  • Joined: 12-December 12

Re: Random not so random?

Posted 15 June 2013 - 03:16 PM

I apologise in advance that this is completely off-topic, but I thought it interesting that we can create an alphabet (an array of Chars) fairly easily:

        Dim letts() As Char = Enumerable.Range(1, 26).
            Select(Function(x) Chr(x + 64)).ToArray

        For Each lett In letts
            Console.WriteLine(lett.ToString)

        Next

Actually, it may be useful in the current context.

This post has been edited by andrewsw: 15 June 2013 - 03:21 PM

Was This Post Helpful? 0
  • +
  • -

#41 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1052
  • View blog
  • Posts: 4,077
  • Joined: 02-July 08

Re: Random not so random?

Posted 15 June 2013 - 03:36 PM

This becoming an advanced topic, we are spending a lot more time discussing methodologies rather than a mere solution to a problem. Maybe we move it there, thoughts?
Was This Post Helpful? 0
  • +
  • -

#42 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1954
  • View blog
  • Posts: 4,055
  • Joined: 11-December 07

Re: Random not so random?

Posted 15 June 2013 - 03:58 PM

What do you reckon, is an array better than a function?

private function alphabetChar(i as integer) as char
    return Chr(x + 64))
end function


You can use that to get a random letter.

private function randomLetter() as Char
    dim lettersInAlphabet = 26
    dim position = rnd.Next(0, lettersInAlphabet)
    return alphabetChar(position)
end function


Or just combine them if you never call alphabetChar on its own:

private function randomLetter() as Char
    dim lettersInAlphabet = 26
    dim position = rnd.Next(0, lettersInAlphabet)
    return Chr(position  + 64))
end function

Was This Post Helpful? 0
  • +
  • -

#43 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 109
  • View blog
  • Posts: 603
  • Joined: 01-October 08

Re: Random not so random?

Posted 16 June 2013 - 05:43 AM

View Postandrewsw, on 15 June 2013 - 05:16 PM, said:

I apologise in advance that this is completely off-topic, but I thought it interesting that we can create an alphabet (an array of Chars) fairly easily:

        Dim letts() As Char = Enumerable.Range(1, 26).
            Select(Function(x) Chr(x + 64)).ToArray

        For Each lett In letts
            Console.WriteLine(lett.ToString)

        Next

Actually, it may be useful in the current context.


Does it get much easier than this?

        'an alphabet, an array of chars
        Dim alphas As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        For Each letter As Char In alphas
            Debug.WriteLine(letter)
        Next



For small sets, say 100 items or less, I question whether there is much of a performance difference in list, dictionary, hash, etc.
Was This Post Helpful? 0
  • +
  • -

#44 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3371
  • View blog
  • Posts: 11,420
  • Joined: 12-December 12

Re: Random not so random?

Posted 16 June 2013 - 07:34 AM

It may not be easier but it is more interesting ;). However, I was thinking that mine could become a function to supply subsets of characters, supplying arguments (65, 90) as an example.
Was This Post Helpful? 0
  • +
  • -

#45 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2257
  • View blog
  • Posts: 9,447
  • Joined: 29-May 08

Re: Random not so random?

Posted 16 June 2013 - 02:06 PM

But that assumes the person understands 65 is A and 90 is Z.
Using Exts.Ranging

Dim Alphabet = "A"c.To("Z"c)


Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4