1

Hi im trying to make it so I can generate a random number 1-35 so for example if the number is 25 it will write out in a string 25 equal signs. How can I do that?

Random r = new Random(); r.next(1, 35); R's result = 25 string result = 25 equal signs 
2
  • 4
    You can try with new string('=', r.Next(1, 35)) but really I didn't understand what you mean. Commented Mar 5, 2017 at 19:24
  • 1
    Make this an answer, @AlessandroD'Andria. Commented Mar 5, 2017 at 19:26

3 Answers 3

7

Class string has a constructor that can do the work for you.

Random r = new Random(); int number = r.next(1, 35); string result = new string('=', number); 
Sign up to request clarification or add additional context in comments.

Comments

0

Note also that it should be r.Next() not r.next().

Random r = new Random(); int occurrences = r.Next(1, 35); StringBuilder sb = new StringBuilder(); for (int i = 0; i < occurrences; i++) { sb.Append('='); } string output = sb.ToString(); Console.WriteLine(output); 

Comments

-1

You need a loop to repeat adding = to your result. Update your code to

Random r = new Random(); int total = r.next(1, 35); string result = ""; for (int i = 0; i < total; i++) { result += "="; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.