Jump to content

  • Log In with Google      Sign In   
  • Create Account


xna random number in 2d


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 burnt_casadilla   Members   -  Reputation: 440

Like
0Likes
Like

Posted 26 April 2013 - 01:25 AM

Im trying to create a random number within the bounds of my screen and when the player comes in contact with the object, a new random object will appear on the screen. I can't for the life of me figure out how to create a random Vector2 value tho -.- I want the random number to be generated within a method so I can also create a bounding box within the method for collision purposes


If you see a post from me, you can safely assume its C# and XNA :)


Sponsor:

#2 Vortez   Crossbones+   -  Reputation: 2698

Like
0Likes
Like

Posted 26 April 2013 - 02:29 AM

I don't know what's tripping you up, the fact that you have 2 random number to generate, or the conversion from int to float?

 

This thread show you how to use the random class, and this one for the conversion.



#3 Andy474   Members   -  Reputation: 677

Like
1Likes
Like

Posted 26 April 2013 - 02:35 AM

Random r = new Random(DateTime.Now.Millisecond);
Vector2 randomVector = new Vector2(r.Next(0, SCREENWIDTH), r.Next(0, SCREENHEIGHT));


#4 LordRhys   Members   -  Reputation: 350

Like
0Likes
Like

Posted 26 April 2013 - 08:37 AM

why are you trying to create a bounding box within the method that your creating a new random object? Your object class should already be able to create a bounding box for itself using the vector location that you provide for it. If you use the code code above provided by Andy474 and pass that into your call for a 'New Object' it should have it's bounding box otherwise the question your asking is not what your looking for.

#5 Nick Gravelyn   Members   -  Reputation: 850

Like
2Likes
Like

Posted 26 April 2013 - 11:04 AM

Random r = new Random(DateTime.Now.Millisecond);
Vector2 randomVector = new Vector2(r.Next(0, SCREENWIDTH), r.Next(0, SCREENHEIGHT));

I would make two notes here:

 

1) The default constructor for Random already handles seeding by time; you don't need to do it yourself.

2) Because of #1, you want to reuse the same Random instance as much as possible. For example if you ran this:

 

for (int i = 0; i < 100; i++)
{
   Random r = new Random();
   someVectorList.Add(new Vector2(r.Next(0, ScreenWidth), r.Next(0, ScreenHeight));
}

 

You would find that quite a few (if not all) vectors would have the exact same values. This is because each new random instance would have the same seed and therefore generate the same sequence of numbers.

 

So in general you want to create one Random per method at the least, but most people generally just make one Random instance as a public static member on some type and reuse that everywhere.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS