Public Group

# A quick question concerning logic

This topic is 3559 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all, I am trying to create a set of 2D random numbers that range from -1 to 1. The values can be as follows: x | y 0 | 1 0 | -1 1 | 0 -1 | 0 Right now I have:
	int X = (rand()%3 - 1); // range from -1 to 1
int Y = (rand()%3 - 1);

This sort of works but it will still give unwanted values like (0,0), (-1,-1) etc. Is there anyway to overcome this problem? I tried using a few if statements and it will always give me a same set of results if I generate a lot of iteration of (x.y). I want to generate a few iterations of that 2d number with the given condition. Hope someone could give some pointers. Thanks! Please don't mark threads 'solved' in this forum. -- jpetrie [Edited by - jpetrie on April 20, 2009 10:25:51 AM]

##### Share on other sites
You could just generate a number from 0 to 3 and use that to choose between (0, 1), (0, -1), (1, 0) and (-1, 0).

##### Share on other sites
Hmmm, noticed that every iteration generates the same numbers :S Can anyone please see if there's anything wrong with this code?
void random(int i){	static int X = (rand()%3 - 1); // range from -1 to 1static int Y = (rand()%3 - 1);if (tempXX == tempYY) tempXX = (rand()%3-1);pos.x = X;pos.y = Y;printf("Seed no : %d pos.x value = %d pos.y value = %d\n\n", i, pos.x, pos.y);}for(int i=1; i<10; i++){random(i);}

Now everytime I ran random I will get 10 sets of the same numbers. Is this just the law of probability? o.0 I seeded the random number generator with the current system time as well.

##### Share on other sites
Quote:
 Original post by SiCraneYou could just generate a number from 0 to 3 and use that to choose between (0, 1), (0, -1), (1, 0) and (-1, 0).

Hmm, I am sorry but I am not so sure what you mean by that. Could you kindly explain please?

edit:

Ahh I think I know what you mean. Thanks, will try that out.

##### Share on other sites
If the random number is 0, then your point is (0, 1). If it's 1, then the point is (0, -1). If it's 2, then (1, 0) and if it's 3, then (-1, 0).

##### Share on other sites
Hmm, can anyone please tell me whats wrong with this code? It keeps generating the same set of numbers:
void random(int i){		static int tempX = ((MaxX-3) - (MinX+3));	static int tempZ = ((MaxZ-3) - (MinZ+3));	static int finalX = tempX; 	static int finalZ = tempZ;		position.x = rand()%finalX - MaxX;	position.y = 0;	position.z = rand()%finalZ - MaxZ;		static int rand1 = rand()%4;	if (rand1 == 0)	{		orient.x = 0;		orient.z = 1;	}	else if (rand1 == 1)	{		orient.x = 0;		orient.z = -1;	}	else if (rand1 == 2)	{		orient.x = 1;		orient.z = 0;	}	else if (rand1 == 3)	{		orient.x = -1;		orient.z = 0;	}	printf("Seed no : %d  orient.x = %d orient.z value = %d\n\n pos.x : %d, pos.z: %d", i, orient.x, orient.z);}for(int i=1; i<10; i++){random(i);}

When I run the iterations, the positions gives nice random numbers but I will get the same set for orient values...

##### Share on other sites
Meh, I think I solved it myself :P Must be tired after programming for so many hours. It's the static int thats doing it. This code should work:

void random(){	static int rand1 = 0;        rand1 = rand()%4;	if (rand1 == 0)	{		orient.x = 0;		orient.z = 1;	}	else if (rand1 == 1)	{		orient.x = 0;		orient.z = -1;	}	else if (rand1 == 2)	{		orient.x = 1;		orient.z = 0;	}	else if (rand1 == 3)	{		orient.x = -1;		orient.z = 0;	}

Cheers for the pointers anyway! Thanks!

##### Share on other sites
Why are you making the variable "rand1" static? Can you tell me what you think "static" does, and what benefit you get from using it in your function?

Here is a simpler solution. Create an array of the acceptable return values and return a random element of the array:
struct Point // or whatever describes the object{   int x, y;};Point random(){    static const Point points [] =     {        { 0, 1},        { 0,-1},        { 1, 0},        {-1, 0},    };    return points[rand() % 4];}

##### Share on other sites
Quote:
 Why are you making the variable "rand1" static? Can you tell me what you think "static" does, and what benefit you get from using it in your function?Here is a simpler solution. Create an array of the acceptable return values and return a random element of the array:

Hi!

Thanks for your suggestion. It's indeed a more elegant solution. However, in my case I will not be repeating the task other then to retrieve those four random points. I guess its a bad practice but I tend to use static to declare the temporary variables that I will be using and initializing within a function. For example :

int Foo(){static int count = 0;if(!Test)count++if(count == 0) return 0;}

So instead of having to declare count (which I will probably just use once) I can just use static. I will read up more on static and I guess you learn from mistakes! Thanks for pointing that out though.

p/s: How did you do the code box? I'm stuck with  tags :( 
 0 
 Share this post Link to post Share on other sites 
 
 
 SiCrane    11839 SiCrane Moderator 11839 Posted April 19, 2009 You can use the [source] tags to get the white code box.Quote:Original post by jonathancSo instead of having to declare count (which I will probably just use once) I can just use static.That makes no sense. 0 Share this post Link to post Share on other sites 
 Prev 1 2 Next Page 1 of 2   Sign in to follow this   Followers 0 
 Go To Topic Listing Forum 
 Advertisement 
 Advertisement What is your GameDev Story? In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us. (You must login to your GameDev.net account.) Share My Story Popular Tags 2D 3D Advice C# C++ Character Concept DX11 DX12 GameMaker Gameplay General Graphics Learning Mobile Music OpenGL Optimization PC Pixel SFX Unity Unreal VR Vulkan Popular Now 13 Unity Updates Terms of Service in Response to Developer Backlash By khawkStarted 8 hours ago 9 DX11 Weird depth buffer values By jakovoStarted Monday at 04:50 PM 15 DX11 Texture repeat question By DividedByZeroStarted Monday at 03:07 AM 14 3D How to get the 3D position for the point with (0,0) UV coordinates? By Stackmann0Started Friday at 06:38 PM 46 In Light of Unity TOS Changes, Epic Games and Improbable Reaffirm Commitment to Devs By khawkStarted Friday at 03:24 AM Advertisement Forum Statistics Total Topics 634057 Total Posts 3015283 GameDev.net GameDev.net Articles GameDev.net Event Coverage GameDev.net Forums GameDev.net Blogs GameDev.net Gallery GameDev.net News GameDev.net Projects GDNet Chat All Activity Search In Everywhere This Forum This Topic More options... Find results that contain... All of my search term words Any of my search term words Find results in... Content titles and body Content titles only Home Groups For Beginners Forum A quick question concerning logic 
 
 
 × Existing user? Sign In Sign Up Browse Back Articles & Tutorials Back All Categories Audio Business Game Design Industry Programming Visual Arts Columns Back GameDev Unboxed Event Coverage Back All Events Game Developers Conference Power Up Digital Games Conference GameDev.Market Links News Podcasts Back All Podcasts Game Dev Loadout Archive Community Back Beginners Back Beginners Group Beginners Forum Beginners Resources Blogs Calendar Chat Forums Back All Forums Audio Business Game Design Programming Visual Arts Community GameDev Challenges Affiliates Topical Workshops Gallery Groups Back For Beginners GameDev Challenges All Groups Projects Back All Projects Games Game Assets Game Mods Developer Tools Store Forums Back All Forums For Beginners Audio Back Music and Sound FX Games Career Development Business Back Games Career Development Production and Management Games Business and Law Game Design Back Game Design and Theory Writing for Games Programming Back Artificial Intelligence Engines and Middleware General and Gameplay Programming Graphics and GPU Programming Math and Physics Networking and Multiplayer Visual Arts Back 2D and 3D Art Critique and Feedback Community Back GameDev Challenges GDNet Lounge GDNet Comments, Suggestions, and Ideas Coding Horrors Your Announcements Hobby Project Classifieds Indie Showcase Affiliates Back NeHe Productions AngelCode Topical Workshops Careers Back Contractors Hobby Projects Game Jobs Back Browse on GameDev.Jobs Post a Job Members Back Subscriptions Chat Guidelines Leaderboard Online Users Awards Search Back All Activity My Activity Streams Back Latest Topics Featured Blogs Search Important Information By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.   I accept GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry. Sign me up! 
 $('body').click(function (e) { var container =$("#pagecontainer"); if (($(e.target).parent().prop('nodeName') == 'BODY') && (container.has(e.target).length === 0) && (e.button == 0) && (!$(e.target).hasClass('ipsDialog'))) { window.open('https://ad.doubleclick.net/ddm/trackclk/N129002.1825GAMEDEV.NET/B11085475.236262913;dc_trk_aid=433791501;dc_trk_cid=90245747;dc_lat=;dc_rdid=;tag_for_child_directed_treatment=;tfua='); ga('send','event','Advertisement','Click','3q_fb_gamedev_skin_2019_1'); } }); $(document).ready(function() { setInterval(function() { window.googletag.pubads().refresh(); }, 30000); });$(document).ready(function() { if (ipsSettings.memberID > 0) { ga('send','event','User','View','Member'); } else { ga('send','event','User','View','Guest'); } });