Results 1 to 12 of 12

Thread: Using time to determine a 'random' number

  1. #1
    .Net Member dday9's Avatar
    Join Date
    Mar 11
    Location
    South Louisiana
    Posts
    2,187

    Using time to determine a 'random' number

    Sometimes an opportunity presents itself where you have two answeres and it doesn't really matter which one you use. A good example could be find b in the y=mx+b formula to determain the equation of line between two points. Or in my example, asking "Which came first, the chicken or the egg?"

    VB.Net Code:
    1. 'Here I just store two answeres
    2.         Dim str() As String = {"The Chicken", "The Egg"}
    3.  
    4.         'Here I get the millisecond
    5.         Dim timeNow As Integer = DateTime.Now.Millisecond
    6.  
    7.         'Here I use the Mod operator to check if the millisecond is odd or even
    8.         If CBool(timeNow Mod 3) Then
    9.             MessageBox.Show(str(0), "Which came first?")
    10.         Else
    11.             MessageBox.Show(str(1), "Which came first?")
    12.         End If

  2. #2
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 08
    Location
    On the Internet
    Posts
    2,846

    Re: Using time to determine a 'random' number

    I'm a little curious as to why you didn't make use of the Random() class, but, you're using time to determine a choice (which, funnily enough, the Random() class has an overload to take a seed, while, if I recall correctly, the default constructor will use the system time as the seed). While that is technically psuedo-random, it can be abused to to get a desired result. Otherwise, a nice small snippet to the Codebank. I recommend you find a way to wrap this up in a class and allow someone to give it a List(Of T) and, using that method, try to see if it will give you random choices.

    A nice little exercise, if you ask me.

  3. #3
    .Net Member dday9's Avatar
    Join Date
    Mar 11
    Location
    South Louisiana
    Posts
    2,187

    Re: Using time to determine a 'random' number

    Thanks for the reply. Reason I didn't use the Random class is because I try to avoid as much as possible. I know in many cases it's best to use Random, but in the case of determain the equation of line example I gave above I was able to avoid it. I will try to wrap it up in a class, seems challenging what you've proposed... I like it! But again, thanks for the reply.

  4. #4
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 08
    Location
    On the Internet
    Posts
    2,846

    Re: Using time to determine a 'random' number

    Quote Originally Posted by dday9 View Post
    Thanks for the reply. Reason I didn't use the Random class is because I try to avoid as much as possible. I know in many cases it's best to use Random, but in the case of determain the equation of line example I gave above I was able to avoid it. I will try to wrap it up in a class, seems challenging what you've proposed... I like it! But again, thanks for the reply.
    Just curious, why do you want to avoid it? Is there a particular reason you dislike it?

  5. #5
    .Net Member dday9's Avatar
    Join Date
    Mar 11
    Location
    South Louisiana
    Posts
    2,187

    Re: Using time to determine a 'random' number

    Because there is never truely a random number. Everything happens because an event triggered it to happen. Not anything to the .Net random, just a personal knitpick.

  6. #6
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 08
    Location
    On the Internet
    Posts
    2,846

    Re: Using time to determine a 'random' number

    Quote Originally Posted by dday9 View Post
    Because there is never truely a random number. Everything happens because an event triggered it to happen. Not anything to the .Net random, just a personal knitpick.
    In all fairness, you can't get true randomness with this either, in any language. You can get really good randomization (Blum Blum Shub, and Mersenne Twister are two good implementations of very good, strong randomization algorithms) if you know what you're doing.

    If you really want extremely good (but slow) randomization, you're going to need hardware to do it. Otherwise, the .NET randomization class (which I believe implements a Linear Congruential Generator, described here) is pretty good for most needs.

  7. #7
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 08
    Location
    On the Internet
    Posts
    2,846

    Re: Using time to determine a 'random' number

    Also, if you want examples for the Mersenne Twister and Blum Blum Shub, I'll be more than happy to post them in the codebank later today when I get home from work.

  8. #8
    .Net Member dday9's Avatar
    Join Date
    Mar 11
    Location
    South Louisiana
    Posts
    2,187

    Re: Using time to determine a 'random' number

    Mais yeah, I love to see some examples. I love studying the art of "randomizing"

  9. #9
    Frenzied Member
    Join Date
    Jul 06
    Location
    MI
    Posts
    1,520

    Re: Using time to determine a 'random' number

    Quote Originally Posted by dday9 View Post

    VB.Net Code:
    1. 'Here I use the Mod operator to check if the millisecond is odd or even
    2.         If CBool(timeNow Mod 3) Then ...
    You need to use Mod 2 to check for odd/even, not Mod 3.

  10. #10
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,801

    Re: Using time to determine a 'random' number

    Quote Originally Posted by dday9 View Post
    Because there is never truely a random number. Everything happens because an event triggered it to happen. Not anything to the .Net random, just a personal knitpick.
    That really doesn't make sense. How is your algorithm any more random than using the Random class? You know ahead of time exactly what value will be returned for every time of the day.

  11. #11
    Frenzied Member boops boops's Avatar
    Join Date
    Nov 08
    Location
    Holland/France
    Posts
    1,980

    Re: Using time to determine a 'random' number

    Hi dday, adding to your woes:

    1. It's better to use DateTime.Ticks because a lot of code can be processed in a millisecond. Your code with messageboxes won't show this because you stop the code, but in other circumstances you could easily end up with all eggs or all chickens!

    2. The example of the line formula y = mx + b is not good because it has only one solution for b. There are other line formulas involving angles where there could be two solutions, corresponding to the directions AB or BA. You would usually use those when the direction does matter. If it really doesn't matter, you don't need a randomizer because you can take whichever solution comes first.

    BB

  12. #12
    .Net Member dday9's Avatar
    Join Date
    Mar 11
    Location
    South Louisiana
    Posts
    2,187

    Re: Using time to determine a 'random' number

    Thanks for all of y'alls suggestions and input. I know that this isn't "random", but I did find it unique. I will state again, that I have a huge intrest in randoms and randomizing. This is just an extension of my intrest.

Posting Permissions

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