Answered need help with the random method

SGD2020

New member
Joined
Jun 9, 2020
Messages
4
Programming Experience
Beginner
I am new to programming and C# and i wanted to create an app for my friend's son to help him with grammar and vocab ,,,, i was thinking of making a random mab libs generator i just cant figure out how to randomize stories rather than numbers if that is even possible any help will be greatly appreciated and thank you in advance
 
Whenever you want to do anything random, it means generating a random number and then using that number in whatever manner is appropriate for your specific scenario. Often times, that means creating a list of items and then generating a random index into that list, e.g. you can select a person at random from a group by putting all the names in a list and then generating a random number greater than or equal to zero and less than the number of names.

What exactly do you mean by "randomize stories"? Do you mean having a list of stories and selecting a random one, or do you maybe mean having a story template and then inserting a random noun here and a random verb there? I'm guessing the latter, but the principle is still the same. You would have a list of nouns and you would generate a random index into that list and then insert the noun at that index into your template.
 
E.g.
C#:
var nouns = new[] { "ball", "horse", "artichoke" };
var verbs = new[] { "bounce", "ride", "eat" };
var names = new[] { "Peter", "Paul", "Mary" };

var rng = new Random();

var nounIndex = rng.Next(0, nouns.Length);
var verbIndex = rng.Next(0, verbs.Length);
var nameIndex = rng.Next(0, names.Length);

Console.WriteLine($"I will {verbs[verbIndex]} the {nouns[nounIndex]} with {names[nameIndex]}.");
 
not exactly what i was looking for but this will help thank you ,,,, what im trying to do is write out about 150 different mad libs stories i want the program to randomly pick a story for him to fill out and read the final print with all of his inputs for verbs and nouns added ,,,, is it possible to put each story with all of the user input questions as private variables in its own method and call the methods randomly so everytime he opens the program it will just pick a random story from the 150 stories i added for him to put his own input in
 
As I already said, if you have a list of items then you generate a random index into that list and get the item at that index. Boom! Random item. That item can be whatever you need it to be. If you need to define a type and create an instance for each story that includes the template text and these questions you mention (sorry, no idea what "mad libs" are) then do that.
 
ok that definitely helps thank you much appreciated ,,,, a mad lib is a story where they ask you to enter verbs, nouns, adjectives, names before you know the story so that the story will be funny because the vocab context is not exactly what it should be they are pretty fun to do and can help kids learn how grammar and vocabulary and have a little fun at the same time
 
If the questions are the same every time then all you need is the text with placeholders that can be easily identified, e.g.
I will |verb| the |noun| with |name|.
And you just call string.Replace the appropriate number of times. If the questions will different each time then you can either parse the text for placeholders to see what questions to ask or define a type that can store the text and the questions.
 
Back
Top Bottom