Get random int, but number can't be near specific range?
Hello. I want to generate a random number but it can't be within 35 of a coordinate.
I'm generating two numbers here. One for x and the other for y. I want x to be 0-720. But say the x coordinate of my character is currently 500. It cant generate the number 465-535. And the same thing in mind when generating the y coordinate.
How would I go about doing this?
Get random int, but number can't be near specific range?
Hello. I want to generate a random number but it can't be within 35 of a coordinate.
I'm generating two numbers here. One for x and the other for y. I want x to be 0-720. But say the x coordinate of my character is currently 500. It cant generate the number 465-535. And the same thing in mind when generating the y coordinate.
How would I go about doing this?
Re: Get random int, but number can't be near specific range?
Make a check
if genNumber + 35 = 500 Then regen()
if genNumber - 35 = 500 Then regen()
get the idea?
Re: Get random int, but number can't be near specific range?
Code:
public static Point GetCoord()
{
Point ReturnCoord = new Point();
Random rnd = new Random();
//get x coord
ReturnCoord.X = rnd.Next(0, 721);
ReturnCoord.Y = rnd.Next(0,721);
while ((ReturnCoord.Y > (ReturnCoord.X - 35)) && (ReturnCoord.Y < (ReturnCoord.X + 35)))
{
//get another value for the Y coordinate
ReturnCoord.Y = rnd.Next(0, 721);
}
return ReturnCoord;
}
Re: Get random int, but number can't be near specific range?
vb.net Code:
Dim num As Integer = myRandom(720 - 35 * 2 + 1)
If num >= 500 - 35 AndAlso num <= 500 + 35 Then
num += 35 * 2
End If
Re: Get random int, but number can't be near specific range?
Do not post the same question twice. It's against forum rules. If it's a C# question then post in C# only. If it's a VB question then post in VB only. If it's a language-agnostic question then post in .NET Architecture & Design only.
http://www.vbforums.com/showthread.php?t=573688
Re: Get random int, but number can't be near specific range?
Quote:
Originally Posted by
demausdauth
Code:
public static Point GetCoord()
{
Point ReturnCoord = new Point();
Random rnd = new Random();
//get x coord
ReturnCoord.X = rnd.Next(0, 721);
ReturnCoord.Y = rnd.Next(0,721);
while ((ReturnCoord.Y > (ReturnCoord.X - 35)) && (ReturnCoord.Y < (ReturnCoord.X + 35)))
{
//get another value for the Y coordinate
ReturnCoord.Y = rnd.Next(0, 721);
}
return ReturnCoord;
}
That is not very efficient...
Anyways... My only guess would to generate 2 random numbers. 1 being below and the other being above the current value. Then you just have it randomly pick one.
Re: Get random int, but number can't be near specific range?
high6 - Not trying to be nitpicky, not sure I understand how it isn't very effiecient. The OP asked for generating a random number(X) then generating another random number(Y) that cannot be within +- 35 of the first number. That is what I did.
I generate the 2 random numbers for X and Y. (Also your suggestion) Then check the Y against the X to see if it meets the requirements of being outside the range X +-35. If it falls within the range the Y value needs to be regenerated and then rechecked.
I used a Point because it sounds like the OP wants to eventually use the X and Y as a point anyways.
Re: Get random int, but number can't be near specific range?
Quote:
Originally Posted by
demausdauth
high6 - Not trying to be nitpicky, not sure I understand how it isn't very effiecient. The OP asked for generating a random number(X) then generating another random number(Y) that cannot be within +- 35 of the first number. That is what I did.
I generate the 2 random numbers for X and Y. (Also your suggestion) Then check the Y against the X to see if it meets the requirements of being outside the range X +-35. If it falls within the range the Y value needs to be regenerated and then rechecked.
I used a Point because it sounds like the OP wants to eventually use the X and Y as a point anyways.
Ya but it will loop an undefined amount of times when it isn't needed.
Re: Get random int, but number can't be near specific range?
It only loops when the Y value falls between +- 35 of the X value.
Re: Get random int, but number can't be near specific range?
Quote:
Originally Posted by
demausdauth
It only loops when the Y value falls between +- 35 of the X value.
Why bother looping at all if you don't have to? If you can easily guarantee that every generated number will be valid then that's what you should do.
Re: Get random int, but number can't be near specific range?
Perhaps I am just being dense about it, but how do u guarantee that the generated values aren't conflicting with the requirements? Considering the that the second number generated has to be compared to the first in order to find out if it is valid.
I guess I am just having problems seeing how to accomplish the goal that the OP wanted without using a loop of some kind, whether it is a for, do or while. I wouldn't mind being enlightened.
Re: Get random int, but number can't be near specific range?
Quote:
Originally Posted by
demausdauth
Perhaps I am just being dense about it, but how do u guarantee that the generated values aren't conflicting with the requirements? Considering the that the second number generated has to be compared to the first in order to find out if it is valid.
I guess I am just having problems seeing how to accomplish the goal that the OP wanted without using a loop of some kind, whether it is a for, do or while. I wouldn't mind being enlightened.
I think you've misread the question. The value of the new Y doesn't depend on the new X. The value of the new X depends on the old X and the value of the new Y depends on the old Y. If you follow the link I provided to the duplicate thread you'll see one way you can guarantee that every value generated is valid.
Re: Get random int, but number can't be near specific range?
Threads merged, now everyone looks silly.
Re: Get random int, but number can't be near specific range?
I'll conceed that the OP was not very clear in what was needed.