I am looking for the vb.net equivalent of this function: I have no experience of PHP but came across this code which apparently gives you a random image url from google images
function GetRandomImageURL($topic='', $min=0, $max=100)
{
// get random image from Google
if ($topic=='') $topic='image';
$ofs=mt_rand($min, $max);
$geturl='http://www.google.ca/images?q=' . $topic . '&start=' . $ofs . '&gbv=1';
$data=file_get_contents($geturl);
$f1='<div id="center_col">';
$f2='<a href="/imgres?imgurl=';
$f3='&imgrefurl=';
$pos1=strpos($data, $f1)+strlen($f1);
if ($pos1==FALSE) return FALSE;
$pos2=strpos($data, $f2, $pos1)+strlen($f2);
if ($pos2==FALSE) return FALSE;
$pos3=strpos($data, $f3, $pos2);
if ($pos3==FALSE) return FALSE;
return substr($data, $pos2, $pos3-$pos2);
}
Its mostly string manipulation, building up the image url. It picks a pseudo random image between index 0
and 100
on this line
$ofs=mt_rand($min, $max);
The same can be achieved with the Random
class in .Net
Dim rnd As New Random()
Dim ofs As Int = rnd.Next(min, max)
I leave the string manipulation to the OP but its probably better to do it with the StringBuilder
class, or perhaps one String.Format
.
It worth considering if the 0
to 100
bounds are overly abritary.