Random not so random?

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

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

#16 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1057
  • View blog
  • Posts: 4,090
  • Joined: 02-July 08

Re: Random not so random?

Posted 13 June 2013 - 08:57 AM

I get that. I feel my odds are good enough, but don't mind learning a fool proof way either.
Was This Post Helpful? 0
  • +
  • -

#17 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 13 June 2013 - 09:29 AM

View Post_HAWK_, on 13 June 2013 - 10:46 AM, said:

'-' is an illegal character in bindings. I could replace them though.


GUID replacing the -'s sounds good.


    Private Function RndColName() As String
        Dim sb As Guid = Guid.NewGuid()
        Return sb.ToString.Replace("-", "")
    End Function


This post has been edited by dbasnett: 13 June 2013 - 09:33 AM

Was This Post Helpful? 0
  • +
  • -

#18 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Random not so random?

Posted 13 June 2013 - 09:47 AM

I've made a quick test:

        Dim list = New List(Of String)


        For i As Integer = 1 To 10000000 ' that's 10M
            list.Add(RndColName())
        Next

        Console.WriteLine(list.Distinct.Count)


    'and this:
    Private Function RndColName() As String
        'Return Guid.NewGuid.ToString.Replace("-", "")
        
        Dim sb As New StringBuilder
        Static rnd As New Random
        For i As Integer = 0 To 15
            Dim s As Char = alphas(rnd.Next(0, alphas.Length))
            sb.Append(s)
        Next
        Return sb.ToString
    End Function



I've run both variations of RndColName several times (notice the commented first line inside), and both were successful. Seems like you would be safe enough using either of them.
Was This Post Helpful? 0
  • +
  • -

#19 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Random not so random?

Posted 13 June 2013 - 04:17 PM

View Postjon.kiparsky, on 13 June 2013 - 07:56 AM, said:

*You could build a hardware-based random number generator, for example making use of the decay of some radioactive particle or some other fact of the world which is known to be "actually random", but this cannot be a part of a language, so we'll leave it aside

In the past, I've used random things to reseed the generator, though it's only useful for some programs. If the program requires keyboard input, the time of the keystrokes is quite random in the milli- or microsecond range, making it useful for breaking the dependence on a single seed slaved to one timestamp. Other asynchronous events, like mouse clicks, events like WebBrowser.DocumentCompleted, arrival of a serial port packet, arrival of specific data on a serial ports, and so on, can all be relied upon to be worthwhile triggers.

This is, of course, my opinion, and I welcome any corrections of misconceptions I might have.
Was This Post Helpful? 0
  • +
  • -

#20 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7805
  • View blog
  • Posts: 13,198
  • Joined: 19-March 11

Re: Random not so random?

Posted 13 June 2013 - 04:26 PM

So if I get the timing right, I can make your online poker game not be random anymore? Cool!
Was This Post Helpful? 0
  • +
  • -

#21 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Random not so random?

Posted 13 June 2013 - 05:55 PM

View Postjon.kiparsky, on 13 June 2013 - 05:26 PM, said:

So if I get the timing right, I can make your online poker game not be random anymore? Cool!

Yup... of course, most folks aren't able to press keys to millisecond resolution.
Was This Post Helpful? 0
  • +
  • -

#22 Jeff H  Icon User is offline

  • D.I.C Regular

Reputation: 112
  • View blog
  • Posts: 308
  • Joined: 30-January 11

Re: Random not so random?

Posted 13 June 2013 - 10:07 PM

View Post_HAWK_, on 13 June 2013 - 10:19 AM, said:

The purpose I had for this to create what I thought would be a better Binding name for some dynamically loaded DataColumns into a Silverlight DataGrid for importing a csv where the number of columns could be any value. One could have just used a CharArray from which the iteration variable could be used to get the char in that array, but this has limits and not totally dynamic and able to complete it's job no matter what was handed to it. This way I can have the function assign the binding name(also the column name) which is a valid binding name as well. Many thanks for the info.


Sorry if I am way off here and not understanding the problem as I have never messed with Silverlight, but if I am understanding correctly you are just needing unique names. The first thing that comes to mind is to keep static counters and increment them each time a name is needed.

Can they be named
A0
A1
....
A2147483647
B0
...

If so,

Sorry I use C# but here is a little example of the idea and just slapped it together and I spent the 95% of time trying to assign the Char with 'A' instead of "A".

No logic for testing when character reaches Z
Module Module1

    Sub Main()

        For i As Integer = 1 To 10
            Console.WriteLine(UniqueName.GetName())
        Next

        For i As Integer = 1 To 100
            Console.WriteLine(UniqueName.GetName())
        Next


        For i As Integer = 1 To 1000000
            Console.WriteLine(UniqueName.GetName())
        Next

        Console.ReadLine()
    End Sub

End Module

Public Class UniqueName
    Private Shared character As Char = "A"
    Private Shared counter As Integer = -1

    Public Shared Function GetName() As String
        If Not counter < Integer.MaxValue Then
            character = Chr(Asc(character) + 1)
            counter = -1
        End If

        counter = counter + 1
        Return character + counter.ToString()
    End Function

End Class




If you went from A to Z it would give you 2147483621 unique names, and if that is not enough you could add logic to add characters when it reaches Z
....
Z2147483647
AA0
....
AZ2147483647
BA0

Sorry if I misunderstood and not familiar with VB and if shared variables have any "gotchas", etc....
Was This Post Helpful? 0
  • +
  • -

#23 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7805
  • View blog
  • Posts: 13,198
  • Joined: 19-March 11

Re: Random not so random?

Posted 13 June 2013 - 10:12 PM

View Postlar3ry, on 13 June 2013 - 07:55 PM, said:

View Postjon.kiparsky, on 13 June 2013 - 05:26 PM, said:

So if I get the timing right, I can make your online poker game not be random anymore? Cool!

Yup... of course, most folks aren't able to press keys to millisecond resolution.


Actually, I think just about anyone can learn to play a musical instrument if they put their mind to it...
Was This Post Helpful? 0
  • +
  • -

#24 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 14 June 2013 - 07:31 AM

View Postjon.kiparsky, on 13 June 2013 - 05:26 PM, said:

So if I get the timing right, I can make your online poker game not be random anymore? Cool!


No different than any RNG's. Though why would you waste your time?

This post has been edited by trevster344: 14 June 2013 - 07:32 AM

Was This Post Helpful? 0
  • +
  • -

#25 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7805
  • View blog
  • Posts: 13,198
  • Joined: 19-March 11

Re: Random not so random?

Posted 14 June 2013 - 07:45 AM

View Posttrevster344, on 14 June 2013 - 09:31 AM, said:

View Postjon.kiparsky, on 13 June 2013 - 05:26 PM, said:

So if I get the timing right, I can make your online poker game not be random anymore? Cool!


No different than any RNG's. Though why would you waste your time?



Well, now he's letting me actually influence the state of the RNG. This is much, much worse than simply not being random.
Was This Post Helpful? 0
  • +
  • -

#26 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 14 June 2013 - 08:00 AM

That is true but would you actually try? :P
Was This Post Helpful? 0
  • +
  • -

#27 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7805
  • View blog
  • Posts: 13,198
  • Joined: 19-March 11

Re: Random not so random?

Posted 14 June 2013 - 08:26 AM

Quote

That is true but would you actually try?


Depends on if I saw a way to win. One thing you can be sure of is that a loophole like this will always be exploited by someone. People are optimists, and they're clever, and someone will see something that looks like a crack, and they'll try to make use of it. There's a lovely story floating around in the Vast Morass of Text - I'm pretty sure either Knuth or Sedgwick links to it, but I can't find it at the moment - about some guys and the way they cracked slot machines by exploiting a buggy random number generator - it involved buzzers in shoes and an uplink to a computer in a hotel room somewhere, as I recall, and it was horrendously complicated, and they made a lot of money at it.

Anyway, the point is, if you want randomness you have to think about where you're getting your entropy from. If it's from the user, you're potentially allowing the user to insert bias. If you're dealing with a cooperative user, this isn't a huge problem, but if the user can gain something by exploiting this, they will probably find a way to do it.
Was This Post Helpful? 1
  • +
  • -

#28 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 14 June 2013 - 08:38 AM

The RNG you choose should fit the application. In this case we are talking about assigning unique names to columns, which as far as I can tell is not worth the end-users time to try and exploit.
Was This Post Helpful? 0
  • +
  • -

#29 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2021
  • View blog
  • Posts: 4,191
  • Joined: 11-December 07

Re: Random not so random?

Posted 15 June 2013 - 06:54 AM

If you are going for your "probably OK", you need to consider the consequences of a collision. Will a game crash and need to be restarted? Will the results of a statistical computation be incorrect, will people's bank accounts start showing the wrong balance, will it affect supply chains, will incorrect tolerances be carried into the construction of a building or a bridge?

As well as the chance of it going wrong, you have to consider the consequences. For this one, I agree that the best bet is a simple sequence, followed by GUID.

However, a nearly random result can be made safe with something like this.

dim result
do
    result = chooseRandomID
until (isUnique(result))

Was This Post Helpful? 1
  • +
  • -

#30 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1057
  • View blog
  • Posts: 4,090
  • Joined: 02-July 08

Re: Random not so random?

Posted 15 June 2013 - 07:32 AM

I actually added a check like that with a List(Of String) and checked if Not {list}.Contains(newid)
Was This Post Helpful? 0
  • +
  • -

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