954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

random number generator

im having trouble creating a random number generator (like say a dice rolling game) if anyone could possibly show me some code for a random number generator that would be greatly appriciated :mrgreen:

trevs234
Newbie Poster
11 posts since Mar 2005
Reputation Points: 10
Solved Threads: 0
 

Show us what you have. You using rand()?

winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 

well i tried to switch a java dice rolling program to C++ but that had ALOT of problems so i scraped that code and havent been able to figure anything else out yet :sad:

trevs234
Newbie Poster
11 posts since Mar 2005
Reputation Points: 10
Solved Threads: 0
 

Simple solution:

Randomize(); //Required in Borland C++

//Generate a random number between 1 and 6
int random_number = (rand() % 6) + 1;


Andi

Rotak
Newbie Poster
19 posts since May 2005
Reputation Points: 11
Solved Threads: 0
 

>> Randomize(); //Required in Borland C++
How about srand instead? That's available everywhere:

#include <cstdlib>
#include <ctime>
#include <iostream>

int main()
{
  std::srand(static_cast<unsigned>(std::time(0)));

  // Generate 10 rolls of a 6 sided die
  for (int i = 0; i < 10; i++)
    std::cout << std::rand() % 6 + 1 << '\n';
}

On some older compilers, using modulus with rand will give you horrible sequences unless the value comes close to RAND_MAX, in this case 6 isn't even close to close. ;) So you'll see this trick as a workaround:

#include <cstdlib>
#include <ctime>
#include <iostream>

int main()
{
  std::srand(static_cast<unsigned>(std::time(0)));

  // Generate 10 rolls of a 6 sided die
  for (int i = 0; i < 10; i++) {
    int r = int((double(std::rand()) / RAND_MAX) * 6 + 1);
    std::cout << r << '\n';
  }
}
Dogtree
Posting Whiz in Training
233 posts since May 2005
Reputation Points: 35
Solved Threads: 3
 

There is a good tutorial on this site you just have to search for it :D

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

thanks this will help me move faster in my master plan (my game) :D

trevs234
Newbie Poster
11 posts since Mar 2005
Reputation Points: 10
Solved Threads: 0
 

hi,
I think that the below function will help you to generate random number betwwen any two numbers you want .

/*Decleration of random_range function */

int random_range (int lowest_no ,int highest_no ) {
 int range ;
  int rando;
  if (lowest_no > highest_no ) 
    {  int temp =lowest_no ;
       lowest_no =highest_no ;
       highest_no =temp ;
    }
    
  range =highest_no -lowest_no +1;
  rando =(range *rand()/ (RAND_MAX +1));
  return (lowest_no +rando );
  }


<< moderator edit: added [code][/code] tags >>
feda :)

feda
Newbie Poster
5 posts since Apr 2005
Reputation Points: 10
Solved Threads: 0
 

This seams like a good place to post this question, if I want to make some thing that generates a random number out of 1 and 2, how would I check that number? Like I ask the user to enter a number, 1 or 2, and if the user typed in the right number that the random generated some thing happens, and if not some thing else happens. Can some one please give me a few examples of this in C++.

TH 113
Newbie Poster
1 post since May 2010
Reputation Points: 9
Solved Threads: 0
 

Look up srand() and rand() . Then use an if statement.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

Hi,
I have an assignment due very soon and im confused, like really confused on this random number generator thing. I have to create a game where the goal is to get to 100. The players are a human player and computer player. If the player rolls a 2,3,4,5 or 6 they get the option to roll again or hold. If they roll a 1 they get nothing for the round. The computer player however has to roll until he gets either 0 or over 20 while it's upto the human player to do what they want. I also need to include two functions " int PlayerTurn (int) and int ComputerTurn (int) ". Any help with this problem would be greatly appreciated...oh I also have to use a void StartRandom () function and int RandomInt (int, int) function :S

Davidov
Newbie Poster
1 post since May 2010
Reputation Points: 10
Solved Threads: 0
 

I don't see any code nor a specific question we can help with...

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You