473,854 Members | 1,637 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Nov 15 '05 #1
10 5995
Johnny Snead <ca****@gte.net > wrote:
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() ;
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.co m>
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.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
Johnny Snead <ca****@gte.net > wrote:
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() ;
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.co m>
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.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #4
> 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();


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************* *@exisconsultin g.com

"Johnny Snead" <ca****@gte.net > wrote in message
news:Fc******** **********@nwrd dc03.gnilink.ne t...
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.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
Johnny Snead <ca****@gte.net > wrote:
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() ;
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.co m>
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.co m> wrote in message
news:MP******** *************** *@news.microsof t.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_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!
Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2402
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 key and send it via Email. Well after finding out about PHP, and that my host does PHP, I thought it would be quicker and more reliable to generate the keys on the server and send them out again either vai email or from the sales site.
23
4216
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 initialize it with the same parameter. Can this be done with JavaScript? I couldn't find anything in the documentation. Basically, what I want to achieve is to obtain always the same sequence of random numbers for the same given initialization...
14
1474
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
1759
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 0000, 0001 ... ZZZZ. I would ideally like the value to appear to jump around randomly, but still be traced back to an incrementing value. So, for example, while a simple standard mapping might look like: foo(1) => 0000
104
5217
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 better way than to fill an array of range 0... RAND_MAX with pre-computed primes and using the output of rand() to index into it to extract a random prime.
22
3465
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
7705
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 through the code and point me in the correct direction one would be very grateful. Example Input : j1mb0jay Example Output 1 : rZHKZbYZWn/4UgL9mAjN2DUz7X/UpcpRxXM9SO1QkvkOe5nOPEKnZldpsB7uHUNZ Example Output 2 :...
21
13524
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
2106
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
0
9901
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9750
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10744
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10364
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9510
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7909
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5738
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5937
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3182
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.