I'm trying to make a game that creates/seeds a random integer between 1 and 10 (inclusive). I think you get the random number and mod it by 10 and add the result by one or something like that. Also, just an inquiry, is it possible to have two different PRNGs in one file? Also, how does a PRNG work and how is it made? I would like to make my own.
How do you make a random integer?
Page 1 of 13 Replies - 867 Views - Last Post: 02 August 2011 - 07:50 AM
Replies To: How do you make a random integer?
#2
Re: How do you make a random integer?
Posted 02 August 2011 - 05:53 AM
I don't know how you would go about making your own, but considering that c++ already has a library devoted to it, making your own is kinda overkill.
First you include the library
then somehwere in the beginning you seed it.
then you can proceed to use random wherever you want.
For Instance
EDIT: Sorry about that, thanks for the catch
First you include the library
#include <cstdlib>
then somehwere in the beginning you seed it.
srand(time(0));
then you can proceed to use random wherever you want.
For Instance
int upperLimit = 50; // highest number you want is fifty int lowerLimit = 12; // lowest number you want is twelve int result = ( rand() % (upperLimit - lowerLimit) ) + lowerLimit; // the final number, between the two bounds
EDIT: Sorry about that, thanks for the catch
This post has been edited by sk1v3r: 02 August 2011 - 07:55 AM
#3
Re: How do you make a random integer?
Posted 02 August 2011 - 06:29 AM
To know how PRNGs work you could read over my tutorial "Generating Pseudorandom Numbers in C/C++" which talks about how to make a basic kind of random number generator called a Linear Congruential Generator (LCG).
Normally rand() is implemented with an LCG or equivalent because they are fast and reliable.
However there are other kinds of RNGs such as the Mersenne twister
Normally rand() is implemented with an LCG or equivalent because they are fast and reliable.
However there are other kinds of RNGs such as the Mersenne twister
#4
Re: How do you make a random integer?
Posted 02 August 2011 - 07:50 AM
sk1v3r, on 02 August 2011 - 08:53 AM, said:
I don't know how you would go about making your own, but considering that c++ already has a library devoted to it, making your own is kinda overkill.
First you include the library
then somehwere in the beginning you seed it.
then you can proceed to use random wherever you want.
For Instance
First you include the library
#include <random>
then somehwere in the beginning you seed it.
srand(time(0));
then you can proceed to use random wherever you want.
For Instance
int upperLimit = 50; // highest number you want is fifty int lowerLimit = 12; // lowest number you want is twelve int result = ( rand() % (upperLimit - lowerLimit) ) + lowerLimit; // the final number, between the two bounds
rand() is in #include <cstdlib> and not #include <random> which contains the C++ TR1 random number generators.
Page 1 of 1

New Topic/Question
Reply


MultiQuote



|