By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,687 Members | 2,228 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,687 IT Pros & Developers. It's quick & easy.

Help with Random Algorithm

P: n/a
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

Nov 15 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Johnny Snead <ca****@gte.net> wrote:
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 Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #2

P: n/a
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" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Johnny Snead <ca****@gte.net> wrote:
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? 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.
string theNum = iRnd.Next(0,8).ToString();

lblAnswer.Text = iRnd.Next(0,8).ToString(); This outputs a number with a datatype string I want a string output with my random saying to make
the 8 ball complete.
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"?


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 --
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 15 '05 #3

P: n/a
Johnny Snead <ca****@gte.net> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #4

P: n/a
> 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.

--
WildHeart'2k3
Nov 15 '05 #5

P: n/a
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]
- ni**************@exisconsulting.com

"Johnny Snead" <ca****@gte.net> wrote in message
news:Fc******************@nwrddc03.gnilink.net...
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" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Johnny Snead <ca****@gte.net> wrote:
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? 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.
string theNum = iRnd.Next(0,8).ToString();

lblAnswer.Text = iRnd.Next(0,8).ToString(); This outputs a number with a datatype string I want a string output with my random saying to

make the 8 ball complete.
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"?


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
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too


Nov 15 '05 #6

P: n/a
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!
Nov 15 '05 #7

P: n/a
"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Johnny Snead <ca****@gte.net> wrote:
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?


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

Nov 15 '05 #8

P: n/a
J S
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!
Nov 15 '05 #9

P: n/a
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

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

P: n/a
cap10b <ca****@gte.net> wrote:
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];
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.