Skip to content
MPGHThe Dark Arts
/
RegisterLog in
Forum
Community
What's NewLatest posts across the boardTrendingHottest threads right nowSubscribedThreads you follow
Discussion
GeneralIntroductionsEntertainmentDebate FortFlaming & Rage
Board
News & AnnouncementsMPGH TimesSuggestions & HelpGiveaways
More Sections
Art & Graphic DesignProgrammingHackingCryptocurrency
Hacks & Cheats
Games
ValorantCS2 / CS:GOCall of Duty / WarzoneFortniteApex LegendsEscape From Tarkov
+14 moreLeague of LegendsGTA VMinecraftRustROTMGBattlefieldTroveBattleOnCombat ArmsCrossFireBlackshotRuneScapeDayZDead by Daylight
Resources
Game Hacking TutorialsReverse EngineeringGeneral Game HackingAnti-CheatConsole Game Hacking
Tools
Game Hacking ToolsTrainers & CheatsHack/Release NewsNew
Submit a release →Share your cheat, tool, or config with the community.
AINEW
AI Tools
General & DiscussionPrompt EngineeringLLM JailbreaksHotAI Agents & AutomationLocal / Open Models
AI × Gaming
AI Aimbots & VisionML Anti-CheatGame Bots & Automation
Create
AI Coding / Vibe CodingAI Art & MediaAI Voice & TTS
The AI frontier →Where game hacking meets modern machine learning. Jump in.
Marketplace
Buy & Sell
SellingBuyingTradingUser Services
Trust & Safety
Middleman LoungeMarketplace TalkVouch Copy Profiles
Money
Cryptocurrency TalkCurrency ExchangeWork & Job Offers
Start selling →List accounts, services, and goods. Use the middleman to trade safe.
MPGH The Dark Arts

A community for offensive security research, reverse engineering, and AI.

Community

ForumMarketplaceSearch

Account

RegisterLog in

Legal

Privacy PolicyForum RulesHelp & FAQ
© 2026 MPGH · All rights reserved.Built by the community, for the community. For educational purposes onlyContent is shared for security research and education — we don't condone illegal use. You're responsible for complying with applicable laws. Use at your own risk.
Home › Forum › Programming › C++/C Programming › Random Integer Generator

Random Integer Generator

Posts 1–13 of 13 · Page 1 of 1
SpiderByte
SpiderByte
Random Integer Generator
Code:
//Program 2.5 - Using Random Integers
include<iostream>    //Input/Output Stream
#include<cstdlib>    //CstdLibrary
#include<ctime>      //CTime
using std::cin;      //Define cin
using std::cout;     //Define cout
using std::endl;     //Define endl;
using std::rand;     //Define rand;
using std::srand;    //Define srand;
using std::time;     //Define time;

int main(){                    //Initiate function main()
    const int limit1 = 500;    //Upper limit for one set of random values
    const int limit2 = 31;     //Upper limit for another set of values
    
    cout << "First we will use the default sequnce from rand().\n";  //Output
    cout << "Three random integers from 0 to " << RAND_MAX << ": "   //Integer from 0 to 32767
         << rand() << " " << rand() << " " << rand() << endl;        //Random integers
         
    cout << endl << "Now we will use a new seed for rand().\n";      //Output
    srand((unsigned int)time(0));    //Set a new seed
    
    cout << "Three random integers from 0 to " << RAND_MAX << ": "   //Integer from 0 to 32767
         << rand() << " " << rand() << " " << rand() << endl;        //Random integers
         
    cout << endl << "Please press the return (enter) key to exit the program . . .";  //Output
    cin.get();     //Wait for keypress(enter)
    return 0;      //Return successful
    
}
#1 · 20y ago
OU
OutZida
What is it?
What does it do?
#2 · 20y ago
Flawless
Flawless
Yes I'm curious about this too..
#3 · 20y ago
arunforce
[MPGH]arunforce
ROFL, it generates an integer (number).
READ THE TITLE, DAMMIT. >.<
#4 · 20y ago
Flawless
Flawless
Yeah, but what can it be used for?
#5 · 20y ago
kyo
kyo
not a lot of uses. REALY basic...
#6 · 20y ago
Flawless
Flawless
Then why is it in the Sig section?
#7 · 20y ago
arunforce
[MPGH]arunforce
It isn't in Sigs, ROFL.
#8 · 20y ago
kyo
kyo
it used to be, i moved it
#9 · 20y ago
SH
shercipher
Derrrr...it generates a random integer.

EDIT:
Quel nub. This code is shorter and works also (note the C leetness of this code).

Code:
#include <stdlib.h>
#include <time.h>
#define RAND_MAX 500

int main() {
srand(time_t);
return rand(RAND_MAX);
}
/* alternatively you can #include <stdio.h> and printf the rand int */
At least that is what I remember.


Oh and by the way what was with all this?
Code:
using std::cin;      //Define cin
using std::cout;     //Define cout
using std::endl;     //Define endl;
using std::rand;     //Define rand;
using std::srand;    //Define srand;
using std::time;     //Define time;
Look at this - I can combine all of that into one line:

Code:
using namespace std;
And I get all the std:: uberleetness!
#10 · 20y ago
Dave84311
[MPGH]Dave84311
Quote Originally Posted by shercipher
Derrrr...it generates a random integer.
Look at this - I can combine all of that into one line:

Code:
using namespace std;
And I get all the std:: uberleetness!
Not exactly C ne more
#11 · 20y ago
SH
shercipher
bah, the C only applied to that leet program, or logram. This was just a way not to have to state every single function/stream you are using.
#12 · 20y ago
SpiderByte
SpiderByte
Generating Random Numbers

Being able to generate random numbers in a program is very useful. You need to be able to build randomness into game programs, for instance; otherwise, they become boring very quickly. The <cstblib> header defines a function rand() that will generate random integers. To be more precise, it generates pseudo-random integers. Random numbers by definition aren't predictable, so any sequence of numberss produced by a numberical algorithm can't truely be random. It just has the appearance of being so. However, now that you understand that, you'll just refer to the numbers produced by the rand() function as random numbers. Note that rand() isn't actually an outstanding random number generator. For many applications, you'll probably want to use something that's rather more sophisticated.
The rand() function returns a random integer as type int. The function doesn't require any arguments, so you can just use it like this:

int random_value = std:rand(); // A random integer

Your store the integer that's returned by the rand() function here in the variable random_value, but you could equally well use it in an arithmetic expression, for example:

int even = 2*std::rand();

The value returned by the rand() function will be a value that is from 0 to RAND_MAX. RAND_MAX is a symbol that is defined in <cstdlib>. When you use RAND_MAX in your code, the compiler will replace it with an integer value. On my system it represents the value 0x7fff, but on other systems it may have a different value. It can be up to 0x3fffffff, which is the maximum integer you can store in type int. If this was the case, then you couldn't multiply the value produced by rand() by 2, as you did previously, without running the risk of getting an incorrect result.
Because RAND_MAX is defined by a preprocessing macro, it isn't within the std namespace, so you don't need to qualify the name when you use it. Any symbol that's defined by a macro won't be in the std namespace because it isn't a name that refers to something. By the time the compiler gets to compile the code, such a symbol will no longer be present because it will have already been replaced by something else during the preprocessing phase.



Making the Sequence Start at Random

Using rand() as you have so far, the sequence of numbers will always be the same. This is because the function uses a default seed value in the algorithm that generates the random numbers. This is fine for testing, but once you have a working game program, you'll really want different sequences each time the program runs. You can change the seed value that will be used to generate the numbers by passing a new seed value as an integer argument to the srand() function that is defined in <cstdlib>, for example:

std::srand(13); // Set seed for rand to 13

The argument to the srand() function must be a value of type unsigned int. Although the preceding statement will result in rand() generating a different sequence from the default, you really need a random seed to get a different sequence from rand() each time you execute a program. Fortunately, the clock on your computer provides a ready-made source of random seed values.
The <ctime> standard library header defines several functions relating to the data and the time. You'll just look at the time() function here because that's precisely what you need to obtain a more or less random seed value. The time() function returns a value that's the number of seconds that have elapsed since midnight on January 1, 1970, so if you use that as a seed, you can be certain that a program will use a different seed value each time it executes. The value is returned as type time_t, which is a type defined in the standard library to be equivalent to an integer type, usually type long. The return type is specified as type time_t to allow felxability in the type of the return value in different C++ implementations. You can use the time() function to create the seed for a random number sequence like this:

std::srand((unsigned int )std::time(0));

There are a few things that you'll have to take on trust for the moment. The argument to the time() function here is 0. There's another possibility for the argument, but you don't need it here so you'll ignore it. The subexpression (unsigned int) serves to convert the value returned by the time() function to type unsigned int, which is the type required for the argument to the srand() method. Without this, the statement wouldn't compile.
Let's put a working example together that makes use of random number generation.

Code:
//Program 2.5 - Using Random Integers
#include<iostream>
#include<cstdlib>
#include<cmath>
using std::cin;
using std::cout;
using std::endl;
using std::rand;
using std::srand;
using std::time;
// These all can be done by simpley using the using namepsace std, but is better to do it this way in small programs, as the using namespace std can sometimes conflict with your code.

int main(){
    const int limit1 = 500;  // Upper limit for one set of random values
    const int limit2 = 31;   // Upper limit for another set of values

    cout << "First we will use the default sequence from rand().\n";
    cout << "Three random integers from 0 to " << RAND_MAX << ": "
           << rand() << " " << rand() << " " << rand() << endl;

    cout << endl
           << "Now we will use a new seed for rand().\n";
    srand((unsigned int)time(0));  // Set a new seed

    cout << "Three random integers from 0 to " << RAND_MAX << ": "
           << rand() << " " << rand() << " " << rand() << endl;

    cin.get();

    return 0;
}
On my system I get the following output:

Code:
First we will use the defualt sequence from rand().
Three random integers from 0 to 32767: 6344 18467 41 // These will always be the same

Now we will use a new seed for rand().
Three random integers from 0 to 32767: 4610 32532 28452 // These will always be different
Don't try to make me look like I don't know what I'm doing...
#13 · 20y ago
Posts 1–13 of 13 · Page 1 of 1

Post a Reply

Similar Threads

  • Random Integer GeneratorBy SpiderByte in Art & Graphic Design
    6Last post 20y ago
  • Random Number GeneratorBy Iam"iDude" in Visual Basic Programming
    5Last post 18y ago
  • [Release]Random Username GeneratorBy Lipit in Combat Arms Hacks & Cheats
    21Last post 18y ago
  • Random Integer Error? I think?By gwentravolta in Visual Basic Programming
    9Last post 17y ago
  • [Application] Random key generatorBy youngbucks in Visual Basic Programming
    17Last post 17y ago

Tags for this Thread

None