lets say I have a multi-line textbox with 100 lines, how would you go about selecting one of those lines randomly in c# and putting that value in a string.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Of course
rshould be a class or instance variable and not re-newed every time you need this, otherwise it won’t be so random.Edit per comment:
This uses the
Random.Next()overload that allows you to specify a range from which you want to pick a (pseudo-) random number. The0is the inclusive lower bound of the range,textBox1.Lines.Lengthis the exclusive upper bound of the range (which means that that number itself won’t be part of the range) – so you will get numbers from 0 totextBox1.Lines.Length-1.