473,385 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Help with Random Algorithm

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
10 5937
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
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
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
> 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
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
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Trythat | last post by:
This might seem a stupid question but here goes - I wrote a key generator for one of my programs in Visual Basic, so I recieve sales notification via email and then use my program to generate the...
23
by: Thomas Mlynarczyk | last post by:
I remember there is a programming language where you can initialize the random number generator, so that it can - if you want - give you the exactly same sequence of random numbers every time you...
14
by: Nikola | last post by:
I need a function that reads from a txt file and randomly chooses a line from which retrieves a string (with spaces) and returns it to main function. thx
8
by: jason | last post by:
Hello everyone, I am looking for an algorithm that would take an incremental value and map that to a case-inspecific alphanumeric string. However, I don't want the string to simply step through...
104
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a...
22
by: gagan.singh.arora | last post by:
Hi there. I want to generate random numbers with a given probability, say 80% even and 20% odd. Is it possible to implement such an algorithm in C?
22
by: j1mb0jay | last post by:
I have had to create a simple string encryption program for coursework, I have completed the task and now have to do a write up on how it could be improved at a later date. If you could look...
21
by: chico_yallin | last post by:
I just wana make a random id number based on4 digits-for examples?? Thanks in Advance Ch.Yallin
10
by: Fred | last post by:
Hi guys, I was wondering how could i get a random number exemple between 1 and 100 but with n% of getting some value over a number x. Thanks a lot in advance. Fred
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.