|
-
Jun 21st, 2009, 02:32 PM
#1
Thread Starter
Lively Member
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?
-
Jun 21st, 2009, 03:39 PM
#2
Thread Starter
Lively Member
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?
-
Jun 21st, 2009, 03:49 PM
#3
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?
-
Jun 21st, 2009, 03:52 PM
#4
Addicted Member
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;
}
If someone has helped you, please make sure to rate them. 
-
Jun 21st, 2009, 07:11 PM
#5
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
-
Jun 21st, 2009, 07:42 PM
#6
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
-
Jun 22nd, 2009, 07:26 AM
#7
Frenzied Member
Re: Get random int, but number can't be near specific range?
 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.
-
Jun 22nd, 2009, 08:26 AM
#8
Addicted Member
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.
If someone has helped you, please make sure to rate them. 
-
Jun 22nd, 2009, 08:33 AM
#9
Frenzied Member
Re: Get random int, but number can't be near specific range?
 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.
-
Jun 22nd, 2009, 09:12 AM
#10
Addicted Member
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.
If someone has helped you, please make sure to rate them. 
-
Jun 22nd, 2009, 08:40 PM
#11
Re: Get random int, but number can't be near specific range?
 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.
-
Jun 22nd, 2009, 11:49 PM
#12
Addicted Member
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.
If someone has helped you, please make sure to rate them. 
-
Jun 22nd, 2009, 11:59 PM
#13
Re: Get random int, but number can't be near specific range?
 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.
-
Jun 23rd, 2009, 12:10 PM
#14
Re: Get random int, but number can't be near specific range?
Threads merged, now everyone looks silly.
-
Jun 23rd, 2009, 01:24 PM
#15
Addicted Member
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.
If someone has helped you, please make sure to rate them. 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|