Help with Random Algorithm

  • Thread starter Thread starter Johnny Snead
  • Start date Start date
J

Johnny Snead

Hey guys,
Need help with this random sort algorithm

private void cmdQuestion_Click(object sender, System.EventArgs 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
 
Johnny Snead said:
private void cmdQuestion_Click(object sender, System.EventArgs e)
{
Random rnd = new Random(); //initialize rnd to new random object
System.Random iRnd = new System.Random();

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.
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.

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,
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 said:
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.
with a datatype string I want a string output with my random saying to make
the 8 ball complete.
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"?

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)
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
response. You guys are fast !!

Thanks again

Johnny
 
Johnny Snead said:
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.

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?
 
private void cmdQuestion_Click(object sender, System.EventArgs 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();

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.
 
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]
- (e-mail address removed)

Johnny Snead said:
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 said:
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.
with a datatype string I want a string output with my random saying to make
the 8 ball complete.
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"?

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)
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
response. You guys are fast !!

Thanks again

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

C#
ASP.NET
VB.NET
VB6
SQL2K
 
Jon Skeet said:
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?

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/default.asp?url=/library/en-us/dncscol/html/csharp01212002.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
 
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.
 
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_Click(object sender, System.EventArgs e)
{
Random rnd = new Random(); //instantiate the random class
string theNum = rnd.Next(0,8).ToString(); //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
 
cap10b said:
Psuedo code for random magic 8 ball web appl.

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.
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.

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_Click(object sender, System.EventArgs e)
{
int index = rng.Next (answers.Length);
lblAnswer.Text = answers[index];
}
 
Back
Top