Jump to content
  • Advertisement
Sign in to follow this  
zoner7

Produce negative values with a random number generator

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there a random number generator function that will produce negative as well as positive values. Rand() apparently does not. Clearly I can do a work around for this, but I'm curious if a function exist to perform this operation. I am sure there is something

Share this post


Link to post
Share on other sites
Advertisement
Yes, there are. If you mention which programming language you are using, we might be able to direct you to some.

Share this post


Link to post
Share on other sites
A random number between -10 and 10:

#include <iostream>
#include <ctime>
#include <boost/random.hpp>

int main()
{
boost::mt19937 engine(time(0));
boost::uniform_int<int> dist(-10, 10);

std::cout << dist(engine);
}



25 random numbers between -10 and 10:

#include <algorithm>
#include <iostream>
#include <ctime>
#include <boost/bind.hpp>
#include <boost/random.hpp>

int main()
{
boost::mt19937 engine(time(0));
boost::uniform_int<int> dist(-10, 10);

std::generate_n(std::ostream_iterator<int>(std::cout, "\n"), 25, boost::bind(dist, engine));
}

Share this post


Link to post
Share on other sites
True rand does not produce negative values but you can adjust it to .


int max = 10;

rand() % max/2 - max/2;

produces values from -5 to 5

Share this post


Link to post
Share on other sites
Quote:
Original post by tnutty
int max = 10;

rand() % max/2 - max/2;

produces values from -5 to 5

More like rand() % (max+1) - (max/2).

Share this post


Link to post
Share on other sites
I tried this rand() % (Max+1) - (Max/2) +.01, but it doesn't seem to produce any negative values.

Also, is there any way that I can make Max a float less than 1 and still have the program work?

Share this post


Link to post
Share on other sites
0 to -x: -(rand() % (x + 1))

-x to x: (rand() % (-x - x - 1)) - x

-x to y: (rand() % (-y - x - 1)) - x

-x to -y: -((rand() % (y - x + 1)) + x)



Hopefully that'll help.

Share this post


Link to post
Share on other sites
Quote:
Original post by zoner7
Also, is there any way that I can make Max a float less than 1 and still have the program work?

Oh, you want real numbers. Just use a different distribution.

#include <algorithm>
#include <iostream>
#include <ctime>
#include <boost/bind.hpp>
#include <boost/random.hpp>

int main()
{
boost::mt19937 engine(time(0));
boost::uniform_real<double> dist(-1.0, 1.0);

std::generate_n(std::ostream_iterator<double>(std::cout, "\n"),
25, boost::bind(dist, engine));
}





And if you insist on using the deprecated rand() function:

double x = double(rand()) / RAND_MAX; // random number between 0.0 and 1.0
x = 2.0*x - 1.0; // random number between -1.0 and +1.0

Oh and if you want numbers between -1.0 inclusive and +1.0 exclusive, use

double x = rand() / (RAND_MAX + 1.0);
x = 2.0*x - 1.0;


[Edited by - DevFred on July 24, 2009 7:51:09 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!