-
April 27th, 2012, 07:21 PM
#1
[RESOLVED] C# Random Methods
I've been trying to write a C# program that generates random sentences. I've succeeded but I have one problem. For example, the random methods seem to only generate the same 1 random number even though I have 3. Thus, only about 9 sentences can actually be generated. After doing some testing, it appears that some sentences tend to be unique because the random methods generate numbers from a range of (1, 8) up to (1, 10).
How do I make it so the random integers are private, rather than displayed/calculated as 1 integer?
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
/* The program will close if the letter "Q" is entered at the first 'Console.ReadLine'.
* The loops are messy but I will fix that after I get the random number methods working.
*/
string x;
Console.WriteLine("Press any key to start:");
x = Console.ReadLine();
do
{
//These 3 random methods only generate 1 number.
int n;
Random rand1 = new Random();
n = rand1.Next(0, 8);
string[] names = { "Bo ", "Rad ", "Jimbo ", "Bill ", "Waldo ", "Vladislav ", "Tommy ", "Moses " };
int v;
Random rand2 = new Random();
v = rand2.Next(0, 9);
string[] verbs = { "killed ", "ate ", "touched ", "saw ", "talked to ", "brazed ", "threw a stone at ", "challenged ", "tickled " };
int o;
Random rand3 = new Random();
o = rand3.Next(0, 10);
string[] objects = { "a box ", "a wall ", "a stone ", "a machine ", "a pill ", "a fruit", "a zombie ", "a cat ", "a cornflake ", "a mugger " };
string r;
/*The n/o/v integers should be different but they are all the same. 'String[] names' has 9
* words so only about 9 types of sentences are generated, rather than the potential 720 sentences.
*/
Console.WriteLine(names[n] + verbs[v] + objects[o]);
r = Console.ReadLine();
}
while (x != "Q");
}
}
}
The code acts very unpredictable so I believe it is best for anyone who wishes to figure out the problem compile and run it to observe how it works. I am using Visual C# Express 2010.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|