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
xxLoganxx
  • 63
  • 1
  • 8

3 Answers3

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);
Alessandro D'Andria
  • 8,663
  • 2
  • 36
  • 32
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);
C. Helling
  • 1,394
  • 6
  • 20
  • 34
-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 += "=";
}
Armin
  • 220
  • 2
  • 10