Help with Random Algorithm

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Johnny Snead

    #1

    Help with Random Algorithm

    Hey guys,
    Need help with this random sort algorithm

    private void cmdQuestion_Cli ck(object sender, System.EventArg s e)
    {
    Random rnd = new Random(); //initialize rnd to new random object
    System.Random iRnd = new System.Random() ;
    string theNum = iRnd.Next(0,8). ToString();

    lblAnswer.Text = iRnd.Next(0,8). ToString();

    This gives me a random number upon the click event however I need to turn
    the numeric sequence into a string value, pass an index to the random object
    and parse the index against an array or an if else statement or switch.
    Have many examples for a console output but none for a web application
    output. any suggestions.

    Thanks

    Johnny



  • Jon Skeet

    #2
    Re: Help with Random Algorithm

    Johnny Snead <cap10b@gte.net > wrote:[color=blue]
    > private void cmdQuestion_Cli ck(object sender, System.EventArg s e)
    > {
    > Random rnd = new Random(); //initialize rnd to new random object
    > System.Random iRnd = new System.Random() ;[/color]

    Why do you want *two* instances of Random?

    Also, you should store a reference to a single instance and repeatedly
    use that - otherwise if you get two clicks in *very* quick succession
    (unlikely in this case, admittedly - this is more a general principle)
    you may well get the same numbers out, as the seed used by Random is
    the current time.
    [color=blue]
    > string theNum = iRnd.Next(0,8). ToString();
    >
    > lblAnswer.Text = iRnd.Next(0,8). ToString();
    >
    > This gives me a random number upon the click event however I need to turn
    > the numeric sequence into a string value, pass an index to the random object
    > and parse the index against an array or an if else statement or switch.
    > Have many examples for a console output but none for a web application
    > output.[/color]

    Well, you've done the turning the number into a string value - but what
    do you mean by "pass an index to the random object"?

    If you could give a full example of what you want at each stage it
    would help.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Johnny Snead

      #3
      Re: Help with Random Algorithm

      Jon,
      Thanks
      I am trying to make a cheap version of magic 8 ball with the random sayings
      from either a case statement or an if - else statement. I am a VB6 student
      trying to jump to C#.
      I have the sort and output down for a random number now I need to change
      this to a random sort on a string with a single output into a lable. All
      this is in a web application.
      And your right I dont need two instances of random I did not catch that
      until you said something.


      "Jon Skeet" <skeet@pobox.co m> wrote in message
      news:MPG.19c63e c87eab349a98a4c 9@news.microsof t.com...[color=blue]
      > Johnny Snead <cap10b@gte.net > wrote:[color=green]
      > > private void cmdQuestion_Cli ck(object sender, System.EventArg s e)
      > > {
      > > Random rnd = new Random(); //initialize rnd to new random object
      > > System.Random iRnd = new System.Random() ;[/color]
      >
      > Why do you want *two* instances of Random? Your right my mistake
      >
      > Also, you should store a reference to a single instance and repeatedly
      > use that - otherwise if you get two clicks in *very* quick succession
      > (unlikely in this case, admittedly - this is more a general principle)
      > you may well get the same numbers out, as the seed used by Random is
      > the current time.
      >[color=green]
      > > string theNum = iRnd.Next(0,8). ToString();
      > >
      > > lblAnswer.Text = iRnd.Next(0,8). ToString(); This outputs a number[/color][/color]
      with a datatype string I want a string output with my random saying to make
      the 8 ball complete.[color=blue][color=green]
      > >
      > > This gives me a random number upon the click event however I need to[/color][/color]
      turn[color=blue][color=green]
      > > the numeric sequence into a string value, pass an index to the random[/color][/color]
      object[color=blue][color=green]
      > > and parse the index against an array or an if else statement or switch.
      > > Have many examples for a console output but none for a web application
      > > output.[/color]
      >
      > Well, you've done the turning the number into a string value - but what
      > do you mean by "pass an index to the random object"?[/color]

      I was trying to be more OO in my approach to the sort. Instead of just
      going thru a case structure was thinking of passing a reference to an object
      and letting that object generate the random string. (Watching too much Dan
      Ingalls videos I guess)[color=blue]
      >
      > If you could give a full example of what you want at each stage it
      > would help.
      > Hope this explains more about what I am doing and I appreciate your quick[/color]
      response. You guys are fast !!

      Thanks again

      Johnny[color=blue]
      > --
      > Jon Skeet - <skeet@pobox.co m>
      > http://www.pobox.com/~skeet/
      > If replying to the group, please do not mail me too[/color]


      Comment

      • Jon Skeet

        #4
        Re: Help with Random Algorithm

        Johnny Snead <cap10b@gte.net > wrote:[color=blue]
        > I am trying to make a cheap version of magic 8 ball with the random sayings
        > from either a case statement or an if - else statement. I am a VB6 student
        > trying to jump to C#.
        > I have the sort and output down for a random number now I need to change
        > this to a random sort on a string with a single output into a lable. All
        > this is in a web application.[/color]

        I would strongly suggest that you do it as a console application first
        - it'll make it simpler, so you can concentrate on just one problem at
        a time. Now, why do you need to do a "random sort on a string"? What
        exactly *is* a random sort anyway?

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Stefano \WildHeart\ Lanzavecchia

          #5
          Re: Help with Random Algorithm

          > private void cmdQuestion_Cli ck(object sender, System.EventArg s e)[color=blue]
          > {
          > Random rnd = new Random(); //initialize rnd to new random object
          > System.Random iRnd = new System.Random() ;
          > string theNum = iRnd.Next(0,8). ToString();
          >
          > lblAnswer.Text = iRnd.Next(0,8). ToString();[/color]

          If you want to produce good random numbers, do not create a new Random
          number generator at every click event. That will skew the statistical
          properties of the generated samples. You should create the Random object
          when the form is initialised, store it in a private member field and use
          Next every time you need a new number.

          --
          WildHeart'2k3


          Comment

          • Nicholas Paldino [.NET/C# MVP]

            #6
            Re: Help with Random Algorithm

            Johnny,

            Since you have eight sayings, your random number should be between 0 and
            7. Once you generate the random number, you can use that number as the
            index into an array that you populate with the answers to the questions.
            Basically, when your program starts, you can create the array of eight
            strings (indexes 0 through 7) and then use the result from the random number
            generator to choose the index in the array.

            Hope this helps.


            --
            - Nicholas Paldino [.NET/C# MVP]
            - nicholas.paldin o@exisconsultin g.com

            "Johnny Snead" <cap10b@gte.net > wrote in message
            news:Fc_6b.1232 2$98.11138@nwrd dc03.gnilink.ne t...[color=blue]
            > Jon,
            > Thanks
            > I am trying to make a cheap version of magic 8 ball with the random[/color]
            sayings[color=blue]
            > from either a case statement or an if - else statement. I am a VB6[/color]
            student[color=blue]
            > trying to jump to C#.
            > I have the sort and output down for a random number now I need to change
            > this to a random sort on a string with a single output into a lable. All
            > this is in a web application.
            > And your right I dont need two instances of random I did not catch that
            > until you said something.
            >
            >
            > "Jon Skeet" <skeet@pobox.co m> wrote in message
            > news:MPG.19c63e c87eab349a98a4c 9@news.microsof t.com...[color=green]
            > > Johnny Snead <cap10b@gte.net > wrote:[color=darkred]
            > > > private void cmdQuestion_Cli ck(object sender, System.EventArg s e)
            > > > {
            > > > Random rnd = new Random(); //initialize rnd to new random object
            > > > System.Random iRnd = new System.Random() ;[/color]
            > >
            > > Why do you want *two* instances of Random? Your right my mistake
            > >
            > > Also, you should store a reference to a single instance and repeatedly
            > > use that - otherwise if you get two clicks in *very* quick succession
            > > (unlikely in this case, admittedly - this is more a general principle)
            > > you may well get the same numbers out, as the seed used by Random is
            > > the current time.
            > >[color=darkred]
            > > > string theNum = iRnd.Next(0,8). ToString();
            > > >
            > > > lblAnswer.Text = iRnd.Next(0,8). ToString(); This outputs a number[/color][/color]
            > with a datatype string I want a string output with my random saying to[/color]
            make[color=blue]
            > the 8 ball complete.[color=green][color=darkred]
            > > >
            > > > This gives me a random number upon the click event however I need to[/color][/color]
            > turn[color=green][color=darkred]
            > > > the numeric sequence into a string value, pass an index to the random[/color][/color]
            > object[color=green][color=darkred]
            > > > and parse the index against an array or an if else statement or[/color][/color][/color]
            switch.[color=blue][color=green][color=darkred]
            > > > Have many examples for a console output but none for a web application
            > > > output.[/color]
            > >
            > > Well, you've done the turning the number into a string value - but what
            > > do you mean by "pass an index to the random object"?[/color]
            >
            > I was trying to be more OO in my approach to the sort. Instead of just
            > going thru a case structure was thinking of passing a reference to an[/color]
            object[color=blue]
            > and letting that object generate the random string. (Watching too much[/color]
            Dan[color=blue]
            > Ingalls videos I guess)[color=green]
            > >
            > > If you could give a full example of what you want at each stage it
            > > would help.
            > > Hope this explains more about what I am doing and I appreciate your[/color][/color]
            quick[color=blue]
            > response. You guys are fast !!
            >
            > Thanks again
            >
            > Johnny[color=green]
            > > --
            > > Jon Skeet - <skeet@pobox.co m>
            > > http://www.pobox.com/~skeet/
            > > If replying to the group, please do not mail me too[/color]
            >
            >[/color]


            Comment

            • cap10b

              #7
              Re: Help with Random Algorithm

              YES !! exactly...
              will come up with some psuedo code and test it.
              Thanks
              Johnny

              C#
              ASP.NET
              VB.NET
              VB6
              SQL2K

              *** Sent via Developersdex http://www.developersdex.com ***
              Don't just participate in USENET...get rewarded for it!

              Comment

              • Michael Mayer

                #8
                Re: Help with Random Algorithm

                "Jon Skeet" <skeet@pobox.co m> wrote in message
                news:MPG.19c680 eaa9693be098a4d 2@news.microsof t.com...[color=blue]
                > Johnny Snead <cap10b@gte.net > wrote:[color=green]
                > > I am trying to make a cheap version of magic 8 ball with the random[/color][/color]
                sayings[color=blue][color=green]
                > > from either a case statement or an if - else statement. I am a VB6[/color][/color]
                student[color=blue][color=green]
                > > trying to jump to C#.
                > > I have the sort and output down for a random number now I need to change
                > > this to a random sort on a string with a single output into a lable.[/color][/color]
                All[color=blue][color=green]
                > > this is in a web application.[/color]
                >
                > I would strongly suggest that you do it as a console application first
                > - it'll make it simpler, so you can concentrate on just one problem at
                > a time. Now, why do you need to do a "random sort on a string"? What
                > exactly *is* a random sort anyway?[/color]

                My guess is that a "random sort" would be a shuffle? For example, dealing
                cards for a game of poker. Of course, if you're playing for money and tell
                everyone that you are "randomly sorting" the cards, you'll likely get a
                black eye.

                But Eric Gunnerson has a cool iterator that will do a "random sort":
                http://msdn.microsoft.com/library/de...rp01212002.asp

                Having said all that, I don't think that would the appropriate behavior for
                a magic 8 ball app. I would think you'd want each new page load to generate
                a random saying independent from all previous loads, as posted elsewhere in
                this thread.

                mike



                Comment

                • J S

                  #9
                  Re: Help with Random Algorithm

                  All,
                  Everyones suggestions and points are well taken, thanks to evertyone for
                  such professional and courteous treatment.
                  Simple put you guys are the best.

                  JDS
                  more to come working on the pseudo code now.





                  *** Sent via Developersdex http://www.developersdex.com ***
                  Don't just participate in USENET...get rewarded for it!

                  Comment

                  • cap10b

                    #10
                    Re: Help with Random Algorithm

                    All,
                    Psuedo code for random magic 8 ball web appl.
                    I have the random number to correspond to the array index.
                    but the lable output does not work. I need to parse string data to the
                    text output of the lable. And suggestions.

                    Thanks

                    Johnny

                    private void cmdQuestion_Cli ck(object sender, System.EventArg s e)
                    {
                    Random rnd = new Random(); //instantiate the random class
                    string theNum = rnd.Next(0,8).T oString(); //set a random int to a
                    variable
                    lblAnswer.Text = theNum;

                    }

                    class SwitchDemo
                    {
                    public static void Main()
                    {
                    int i;

                    for(i=0; i<9; i++)
                    switch(i)
                    {
                    case 0:
                    return "Not Today";
                    break;
                    case 1:
                    return "Duh !";
                    break;
                    case 2:
                    return "Ask Again";
                    break;
                    case 3:
                    return "In your Dreams";
                    break;
                    case 4:
                    return "If I could I would";
                    break;
                    default:
                    return "Try again later";
                    break;
                    }

                    C

                    *** Sent via Developersdex http://www.developersdex.com ***
                    Don't just participate in USENET...get rewarded for it!

                    Comment

                    • Jon Skeet

                      #11
                      Re: Help with Random Algorithm

                      cap10b <cap10b@gte.net > wrote:[color=blue]
                      > Psuedo code for random magic 8 ball web appl.[/color]

                      Small hint: real code is *much* more helpful. It's hard to tell whether
                      the errors are due to typos in your pseudo-code, or whether those
                      errors are in your real code.
                      [color=blue]
                      > I have the random number to correspond to the array index.
                      > but the lable output does not work. I need to parse string data to the
                      > text output of the lable. And suggestions.[/color]

                      I would suggest you don't convert the random number into a string to
                      start with - I really can't see why you're doing that. I'd have:

                      static readonly string[] answers = {"Not Today", "Duh!", "Ask again",
                      "In your Dreams", "etc..."};
                      static readonly Random rng = new Random();

                      then:

                      void cmdQuestion_Cli ck(object sender, System.EventArg s e)
                      {
                      int index = rng.Next (answers.Length );
                      lblAnswer.Text = answers[index];
                      }

                      --
                      Jon Skeet - <skeet@pobox.co m>
                      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                      If replying to the group, please do not mail me too

                      Comment

                      Working...