Results 1 to 15 of 15

Thread: Get random int, but number can't be near specific range?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Posts
    70

    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?

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Posts
    70

    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?

  3. #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?

  4. #4
    Addicted Member
    Join Date
    Nov 2006
    Location
    Minnesota
    Posts
    235

    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.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Get random int, but number can't be near specific range?

    vb.net Code:
    1. Dim num As Integer = myRandom(720 - 35 * 2 + 1)
    2.  
    3. If num >= 500 - 35 AndAlso num <= 500 + 35 Then
    4.     num += 35 * 2
    5. End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: Get random int, but number can't be near specific range?

    Quote Originally Posted by demausdauth View Post
    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.

  8. #8
    Addicted Member
    Join Date
    Nov 2006
    Location
    Minnesota
    Posts
    235

    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.

  9. #9
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: Get random int, but number can't be near specific range?

    Quote Originally Posted by demausdauth View Post
    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.

  10. #10
    Addicted Member
    Join Date
    Nov 2006
    Location
    Minnesota
    Posts
    235

    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.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Get random int, but number can't be near specific range?

    Quote Originally Posted by demausdauth View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12
    Addicted Member
    Join Date
    Nov 2006
    Location
    Minnesota
    Posts
    235

    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.

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Get random int, but number can't be near specific range?

    Quote Originally Posted by demausdauth View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Get random int, but number can't be near specific range?

    Threads merged, now everyone looks silly.

  15. #15
    Addicted Member
    Join Date
    Nov 2006
    Location
    Minnesota
    Posts
    235

    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
  •  



Click Here to Expand Forum to Full Width