Just a little technical snippet which, as usual, may or may not be useful. However, I believe most seasoned programmers already have something like that in their arsenal.
In previous web tutorials such as The Ngerng Effect and The Random Quote Google Gadget, I used a line of code to generate random numbers. It worked. But it was hellishly difficult to read. And that kind of gets in the way if you're trying to explain a concept.
What we need to do here is make a function for it. I'm going to demonstrate this using JavaScript first. The line for creating a random number is, according to W3Schools:
Math.floor((Math.random() * x)+y)
x is the maximum number you wish to generate, and y is the minimum.
So if we make a function like so...
function generaterandomno(x,y)
{
return Math.floor((Math.random() * y)+x);
}
{
return Math.floor((Math.random() * y)+x);
}
Now I can just call a random number without all the technical mumbo-jumbo. Here, I use JavaScript to create a varianble randomno and assign a random value between 1 to 100 using the generaterandomno() function.
var randomno=generaterandomno(1,100);
However, this formula is flawed. It's perfect if the value of x is 1 or 0, meaning, you only want to generate a random number with a minimum value of 1 or 0, like a random number from 0 to 100, or 1 to 100. Anything greater than 1, and the formula falls flat.
Thus, if you want a random number from, say, 10 to 100, what you really should be doing is generating a number from 0 to 90, and then adding 10 to the result.
Which also means the formula is more like this.
Math.floor((Math.random() * (x-y+1))+y)
The additional +1 is supposed to compensate for the Math.floor() function, which rounds down. Math.random() gets a number between 0 and 1, which is a floating point. And Math.floor() turns the result into an integer.
Note that this probably doesn't handle cases where the value of x and/or y is a negative number. And until I have a reason to find out, we're not going there.
In other languages...
The principle is similar, though the quirks vary from language to language. Generally, the older langugaes seem to have the problem outlined above while newer languages simply get that out of the way.In, PHP, this is how I'd do it. That's because PHP already kindly provides just such a function. Apparently someone in the open-source community decided to simplify the process.
$randomno=rand(1,100);
In C/C++
int generaterandomno(int x, int y)
{
srand (time(NULL));
return (rand() % (y-x+1)) + x;
}
int randomno=generaterandomno(1,100);
{
srand (time(NULL));
return (rand() % (y-x+1)) + x;
}
int randomno=generaterandomno(1,100);
In Java (sans import utility and class notations)
public int generaterandomno(int x, int y)
{
Random rnd= new Random();
return rand.nextInt(y - x + 1) + x;
}
int randomno=generaterandomno(1,100);
{
Random rnd= new Random();
return rand.nextInt(y - x + 1) + x;
}
int randomno=generaterandomno(1,100);
In C# (sans import utility and class notations)
public int generaterandomno(int x, int y)
{
Random rnd = new Random();
return rnd.Next(x,y);
}
int randomno=generaterandomno(1,100);
{
Random rnd = new Random();
return rnd.Next(x,y);
}
int randomno=generaterandomno(1,100);
In ASP
function generaterandomno(x,y)
Randomize
generaterandomno = Int((rnd*(y-x+1)))+x
end function
Dim randomno=generaterandomno(1,10)
Randomize
generaterandomno = Int((rnd*(y-x+1)))+x
end function
Dim randomno=generaterandomno(1,10)
In QBasic
FUNCTION generaterandomno(x AS SINGLE,yAS SINGLE)
RANDOMIZE TIMER
generaterandomno = INT(RND * (y-x+1)) + x
END FUNCTION
DIM randomnogeneraterandomno(1,10)
RANDOMIZE TIMER
generaterandomno = INT(RND * (y-x+1)) + x
END FUNCTION
DIM randomnogeneraterandomno(1,10)
There it is, a list of randomizer functions in some of the languages I remember. A useful list if I ever need to generate random effects.
y still confused? Didn't I x-plain it well enough? (heh heh)
T___T
T___T
No comments:
Post a Comment