Jump to content

I'd like to know if any function that generates random numbers exist, and if not, how will I be able to create them myself.

 

Is it possible to generate a number from one to four specifically? I'm in the process of programming baddies in a simple Pacman-like console game, and depending on the number generated, the bad guy goes up, down, left, or right.

 

BTW, is it possible to program the baddie to move in the direction of the player, every iteration? If yes, please walk me through the process as simple as possible.

 

Thanks!!!

 

 

Nothing to see here ;)

Link to comment
https://linustechtips.com/topic/554740-random-number-generator-in-c/
Share on other sites

Link to post
Share on other sites

I don't think it's a true random but rand() should give you a "random" number.

 

So rand() %4 + 1  should be a "random" number in the range of 1-4

- ASUS X99 Deluxe - i7 5820k - Nvidia GTX 1080ti SLi - 4x4GB EVGA SSC 2800mhz DDR4 - Samsung SM951 500 - 2x Samsung 850 EVO 512 -

- EK Supremacy EVO CPU Block - EK FC 1080 GPU Blocks - EK XRES 100 DDC - EK Coolstream XE 360 - EK Coolstream XE 240 -

Link to post
Share on other sites

3 minutes ago, TidaLWaveZ said:

I don't think it's a true random but rand() should give you a "random" number.

it's not true random, it's actually REALLY not that great. I used it once in a simple programm (blackjack) and the same set of cards is always drawn. for example for a range between 1 and 13, it would almost alwas pick a 6, 7, 8 or 9... i did the math after trying it alone for a lot of time and the results don't match at all the true probabilities of drawing those cards... I reccomend going with something more complex

R5 3600x | RTX 3070 | 16Gb 3200mhz | Gigabyte B550 gaming x | 500gb 660p SSD + 1tb wd blue HDD |

Link to post
Share on other sites

1 minute ago, Epiclol86 said:

it's not true random, it's actually REALLY not that great. I used it once in a simple programm (blackjack) and the same set of cards is always drawn. for example for a range between 1 and 13, it would almost alwas pick a 6, 7, 8 or 9... i did the math after trying it alone for a lot of time and the results don't match at all the true probabilities of drawing those cards... I reccomend going with something more complex

Hmmmm... So what do you suggest I go with? Or you can also help me with the solution to the second option - refer to my first post.

 

Thanks!

Nothing to see here ;)

Link to post
Share on other sites

There are premade functions, rand() being amongst the worst because it is predictable and will produce the same sequence of numbers every time you run the program.

 

Most decent random functions get a lot of data from stuff like the system clock, ram usage and generally other parameters that cannot be easily predicted and change from moment to moment, then pass them through a complex algorythm to try and give all numbers a similar chance of showing up.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to post
Share on other sites

9 minutes ago, Epiclol86 said:

it's not true random, it's actually REALLY not that great. I used it once in a simple programm (blackjack) and the same set of cards is always drawn. for example for a range between 1 and 13, it would almost alwas pick a 6, 7, 8 or 9... i did the math after trying it alone for a lot of time and the results don't match at all the true probabilities of drawing those cards... I reccomend going with something more complex

 

I remember my professor making it very clear that this wasn't a true random, though I don't remember any alternatives.

- ASUS X99 Deluxe - i7 5820k - Nvidia GTX 1080ti SLi - 4x4GB EVGA SSC 2800mhz DDR4 - Samsung SM951 500 - 2x Samsung 850 EVO 512 -

- EK Supremacy EVO CPU Block - EK FC 1080 GPU Blocks - EK XRES 100 DDC - EK Coolstream XE 360 - EK Coolstream XE 240 -

Link to post
Share on other sites

2 minutes ago, Sauron said:

There are premade functions, rand() being amongst the worst because it is predictable and will produce the same sequence of numbers every time you run the program.

 

Most decent random functions get a lot of data from stuff like the system clock, ram usage and generally other parameters that cannot be easily predicted and change from moment to moment, then pass them through a complex algorythm to try and give all numbers a similar chance of showing up.

I've also heard of srand()... is it related to rand()? Does it generate truly random numbers?

Nothing to see here ;)

Link to post
Share on other sites

Just now, anandgeforce said:

I've also heard of srand()... is it related to rand()? Does it generate truly random numbers?

If I recall correctly srand is just a seeding function for rand() - basically the number you feed it changes the starting point of the sequence generated by rand(). However unless you take the seed from something like the system clock it won't make a difference on the randomness.

 

you should use the <random> library instead.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to post
Share on other sites

2 minutes ago, anandgeforce said:

I've also heard of srand()... is it related to rand()? Does it generate truly random numbers?

 

No, srand() is of the same caliber as rand() and will not give you a truly random numbers.

- ASUS X99 Deluxe - i7 5820k - Nvidia GTX 1080ti SLi - 4x4GB EVGA SSC 2800mhz DDR4 - Samsung SM951 500 - 2x Samsung 850 EVO 512 -

- EK Supremacy EVO CPU Block - EK FC 1080 GPU Blocks - EK XRES 100 DDC - EK Coolstream XE 360 - EK Coolstream XE 240 -

Link to post
Share on other sites

1 minute ago, Sauron said:

If I recall correctly srand is just a seeding function for rand() - basically the number you feed it changes the starting point of the sequence generated by rand(). However unless you take the seed from something like the system clock it won't make a difference on the randomness.

 

you should use the <random> library instead.

I prefer to not to use third-party libraries - any DIY solutions that you might come up with?

 

Thanks!

Nothing to see here ;)

Link to post
Share on other sites

I'm not so sure that anything would return a truly random number.

 

If you are using the system clock or something else, then the number is dependent on other information so it wouldn't be random either.

 

Maybe more random, but not completely random. (Sounds ridiculous, but only way I could think to say it)

 

- ASUS X99 Deluxe - i7 5820k - Nvidia GTX 1080ti SLi - 4x4GB EVGA SSC 2800mhz DDR4 - Samsung SM951 500 - 2x Samsung 850 EVO 512 -

- EK Supremacy EVO CPU Block - EK FC 1080 GPU Blocks - EK XRES 100 DDC - EK Coolstream XE 360 - EK Coolstream XE 240 -

Link to post
Share on other sites

3 minutes ago, TidaLWaveZ said:

I'm not so sure that anything would return a truly random number.

 

If you are using the system clock or something else, then the number is dependent on other information so it wouldn't be random either.

 

Is it possible to create a function that uses the system clock to generate a random number? Do you know how to do it?

Nothing to see here ;)

Link to post
Share on other sites

11 minutes ago, anandgeforce said:

Is it possible to create a function that uses the system clock to generate a random number? Do you know how to do it?

It's possible, I don't know off hand how to do it.  I think maybe you would use the system clock as the seed srand(time()) and then use rand().

- ASUS X99 Deluxe - i7 5820k - Nvidia GTX 1080ti SLi - 4x4GB EVGA SSC 2800mhz DDR4 - Samsung SM951 500 - 2x Samsung 850 EVO 512 -

- EK Supremacy EVO CPU Block - EK FC 1080 GPU Blocks - EK XRES 100 DDC - EK Coolstream XE 360 - EK Coolstream XE 240 -

Link to post
Share on other sites

Firstly, you will never create true randomness, all random libraries use algorithms based on a seed to generate their "random" number.

 

I would recommend you watch this video by numberphile about random numbers -

 

Now people here are saying that rand is bad is really dependent on what you want to use it for. The main issue with rand is the size limit of the number (RAND_MAX defined in cstdlib) though for most applications this wont be an issue either.

 

As long as you seed rand before you start using it then I don't see its "randomness" causing any issue for you

 

To add to my previous post an example:


 

unsigned int g_RandomSeed = 0

unsigned int GetRandomNumber()
{
	unsigned int randomNumber = (4517 * g_randomSeed * 7907) % UINT_MAX;
	g_randomSeed = randomNumber;
	return randomNumber;
}

 

Please note this is a terrible random number generation so you really shouldn't use it but it is a basic example of how a random number generation function could look

Link to post
Share on other sites

C++11 added a new <random> library that should be used over rand().  Do not make your own RNG, it will be broken.

Here's an example of using the new <random> to get an int in a specific range http://code.runnable.com/UwC9pWVQ81UYAAAy/a-random-integer-real-number-generator-example-for-c%2B%2B-c%2B%2B11-and-mt19937

 

edit: <random> is standard C++, not from a third party.

1474412270.2748842

Link to post
Share on other sites

1 hour ago, anandgeforce said:

Is it possible to create a function that uses the system clock to generate a random number? Do you know how to do it?

it's not third party, it's standard c++11.  @fizzlesticks is right

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to post
Share on other sites

23 minutes ago, Sauron said:

it's not third party, it's standard c++11.  @fizzlesticks is right

Does Visual Studio 2015 Community have C++ 11?

 

@fizzlesticks : Thank you for the clarification.

Nothing to see here ;)

Link to post
Share on other sites

17 hours ago, fizzlesticks said:

C++11 added a new <random> library that should be used over rand().  Do not make your own RNG, it will be broken.

Here's an example of using the new <random> to get an int in a specific range http://code.runnable.com/UwC9pWVQ81UYAAAy/a-random-integer-real-number-generator-example-for-c%2B%2B-c%2B%2B11-and-mt19937

 

edit: <random> is standard C++, not from a third party.

I checked out your link, but I want to know that piece of code, line-by-line, to truly understand it. Can you please help me with that?

 

Thanks!

Nothing to see here ;)

Link to post
Share on other sites

54 minutes ago, anandgeforce said:

I checked out your link, but I want to know that piece of code, line-by-line, to truly understand it. Can you please help me with that?

 

Thanks!

#include <iostream> //include for cout, endl
#include <random> //include for random_device, mt19937, uniform_int_distribution,
                  //uniform_real_distribution

using namespace std; //horrible line of code that shouldn't be used

int main (int argc, char* argv[]) //normal main function
{
  const unsigned int total = 50; //amount of random numbers to generate
  
  random_device rd; //creates a random_device that tries to generate non-deterministic random numbers
                    //is slow, may fall back to a non-seeded pseudo-RNG (very bad), may not be able to generate an unlimited
                    //amount of numbers but is only being used to seed a pseudo-RNG so those aren't 
                    //problems on most systems


  mt19937 re(rd()); //create a mersenne twister psuedo-RNG engine seeded by a random number from random_device 

  uniform_int_distribution<int> ui(1, 500); //create a uniform distribution of ints between 1 and 500
                                            //uniform distribution = each number has the same chance of being chosen
                                            //other kinds of distributions can be found at http://www.cplusplus.com/reference/random/
  
  for (unsigned int i(0); i < total; ++i)
  {
    cout << ui(re) << endl; //pass a random number engine (our mt19937) to a distribution to generate a random number
  }
  
  cout << endl;
  
  uniform_real_distribution<double> ud(0, 1); //same as above but generates random doubles between 0 and 1
  
  for (unsigned int i(0); i < total; ++i)
  {
    cout << ud(re) << endl; //same as above
  }
  
  return 0;
} //closing cirly brace

 

1474412270.2748842

Link to post
Share on other sites

The first guy answered with what the OP needed and the rest just went to town with it.  Why do people always have to bring up this stuff about pseudorandom, how it's not truly random and so on? You are not wrong. But the OP is probably new to programming and is asking how to pick a random direction for a simple game. I doubt that someone who is just learning about programming needs a lecture about how rand() function is not really random and why they should instead use this huge bunch of, from their point of view unintelligible, code instead.

Link to post
Share on other sites

23 hours ago, fizzlesticks said:

#include <iostream> //include for cout, endl
#include <random> //include for random_device, mt19937, uniform_int_distribution,
                  //uniform_real_distribution

using namespace std; //horrible line of code that shouldn't be used

int main (int argc, char* argv[]) //normal main function
{
  const unsigned int total = 50; //amount of random numbers to generate
  
  random_device rd; //creates a random_device that tries to generate non-deterministic random numbers
                    //is slow, may fall back to a non-seeded pseudo-RNG (very bad), may not be able to generate an unlimited
                    //amount of numbers but is only being used to seed a pseudo-RNG so those aren't 
                    //problems on most systems


  mt19937 re(rd()); //create a mersenne twister psuedo-RNG engine seeded by a random number from random_device 

  uniform_int_distribution<int> ui(1, 500); //create a uniform distribution of ints between 1 and 500
                                            //uniform distribution = each number has the same chance of being chosen
                                            //other kinds of distributions can be found at http://www.cplusplus.com/reference/random/
  
  for (unsigned int i(0); i < total; ++i)
  {
    cout << ui(re) << endl; //pass a random number engine (our mt19937) to a distribution to generate a random number
  }
  
  cout << endl;
  
  uniform_real_distribution<double> ud(0, 1); //same as above but generates random doubles between 0 and 1
  
  for (unsigned int i(0); i < total; ++i)
  {
    cout << ud(re) << endl; //same as above
  }
  
  return 0;
} //closing cirly brace

 

Wow - thanks to you, I understood the code much better now!

Nothing to see here ;)

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×