3 Replies - 867 Views - Last Post: 02 August 2011 - 07:50 AM Rate Topic: -----

#1 #include <me>   User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 3
  • Joined: 02-August 11

How do you make a random integer?

Posted 02 August 2011 - 05:48 AM

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.
Is This A Good Question/Topic? 0
  • +

Replies To: How do you make a random integer?

#2 sk1v3r   User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

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
#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

Was This Post Helpful? 0
  • +
  • -

#3 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

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
Was This Post Helpful? 1
  • +
  • -

#4 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: How do you make a random integer?

Posted 02 August 2011 - 07:50 AM

View Postsk1v3r, 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
#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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1