1
    private void button2_Click(object sender, EventArgs e)
    {
        Process.Start("http://google.com");
    }  

for example, then when clicked again it has a chance of opening like yahoo or even google again. tried

    private void button2_Click(object sender, EventArgs e)
    {
        Process.Start("http://google.com");
        Process.Start("http://yahoo.com");
        Process.Start("http://stackoverflow.com");
    }  

but that opens up all 3 sites at the same time in my default browser I want it to open 1 out of those 3 sites randomly each time the button is clicked.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Unknown
  • 11
  • 4
  • You can use the `Random` class to choose a random number and a `switch` or some `if` statements. Do some googling. – Blorgbeard Oct 04 '15 at 23:52
  • First of all, can you use IP addresses? How else will you get random websites? – entitycs Oct 04 '15 at 23:54
  • dustin i dont want it to be random random, im gonna add the options of websites myself that i want to be randomly chosen to be opened when button is clicked. – Unknown Oct 05 '15 at 00:13
  • Possible duplicate of [C# Select random element from List](http://stackoverflow.com/questions/19318430/c-sharp-select-random-element-from-list) – Alexei Levenkov Oct 05 '15 at 01:38

4 Answers4

2

Use the Random class and limit the random numbers within an interval:

Java sample as original post was not marked with language, for C# - remove final and use System.Random to select value:

private void button2_Click(object sender, EventArgs e)
{
    final String[] urls = {
      "http://google.com",
      "http://yahoo.com",
      "http://stackoverflow.com"
    };

    final int pick = (int)(Math.random() * urls.length);
    Process.Start(urls[pick]);
}  
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
sled
  • 14,525
  • 3
  • 42
  • 70
  • It had no language tag, I added C# based on the code. I don't think OP's code is valid Java? – Blorgbeard Oct 05 '15 at 00:00
  • 1
    `Process` makes use of `IDisposable` which it inherits from `Component`. Just something to keep in mind... as you want to properly dispose of it. – B.K. Oct 05 '15 at 00:10
  • yeah sorry im new to the site forgot to add what language but yes it is c# – Unknown Oct 05 '15 at 00:14
2

You can create a string array to hold the site addresses, like so:

string[] sites = {
    "http://google.com",
    "http://yahoo.com",
    "http://stackoverflow.com" };

And then use the Random class to select one of those sites on your button click:

private void button2_Click(object sender, EventArgs e)
{
    Random random = new Random();
    Process.Start(sites[random.Next(sites.Length)]);
}

The Next method of the random class will return a value less than the specified number, so no chance of an array out of bounds exception

Shadow
  • 3,926
  • 5
  • 20
  • 41
  • 1
    It would be better to show correct usage of `Random`: http://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number (or at least add comment). – Alexei Levenkov Oct 05 '15 at 01:45
  • You'd have to click pretty quickly for this code to fail in that way @AlexeiLevenkov – Blorgbeard Oct 08 '15 at 18:59
0

Random number generator (should be a library for it in c#.

Add in a couple variables with your links, maybe store them in an array.

Then run that line you have with the array at the index that was randomly generated.

CristianD
  • 138
  • 1
  • 12
0
Random rnd = new Random();
int website = rnd.Next(0, numOfWebsites);
switch(website)
{
    case 0:
    {
        Process.Start("http://google.com");
        break;
    }
    case 1:
    {
        Process.Start("http://yahoo.com");
        break;
    }
}

You can use a switch case statement for readability with a random number generator. I apologize if i made any errors as i don't use C# much. A if else statement would also work for a situation like this.

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
Ryan
  • 1,972
  • 2
  • 23
  • 36
  • might have been a copy and paste from another project lol. i'll fix that. – Ryan Oct 05 '15 at 00:00
  • It should also be `rnd.Next(0, numOfWebsites)`, or you'll never hit google (and start with `case 0`). And `Process.Start` with a capital `S`. – Blorgbeard Oct 05 '15 at 00:02
  • 'control cannot fall out of switch from final case label' shows that error for string 0 and 1 – Unknown Oct 05 '15 at 00:21