Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A QuizPlease log in to access this content. You will be redirected to the login page shortly.
LoginComputer Definitions P - Definitions in Computer Definitions 3 years ago
Pseudorandom numbers are generated by computers. They are not truly RANDOM, because when a computer is functioning correctly, nothing it does is random. Computers are deterministic devices — a computer's behavior is entirely predictable, by design. So to create something unpredictable, computers use mathematical algorithms to produce numbers that are "random enough."
When are pseudorandom numbers used?Pseudorandom numbers are essential to many computer applications, such as games and security. In games, random numbers provide unpredictable elements the player can respond to, such as dodging a random bullet or drawing a card from a deck.
In computer security, pseudorandomness is important in encryption algorithms, which create codes that must not be predicted or guessed.
What is a PRNG?A pseudorandom number generator, or PRNG, is any program, or function, which uses math to simulate randomness. It may also be called a DRNG (digital random number generator) or DRBG (deterministic random bit generator).
The math can sometimes be complex, but in general, using a PRNG requires only two steps:
The seed value is a "starting point" for creating random numbers. The value is used when computing the numbers. If the seed value changes, the generated numbers also change, and a single seed value always produce the same numbers. For this reason, the numbers aren't really random, because true randomness could never be re-created.
The current time is often used as a unique seed value. For instance, if it's March 5, 2018, at 5:03 P.M. and 7.01324 seconds UTC, that can be expressed as an integer. That precise time never occur again, so a PRNG with that seed should produce a unique set of random numbers.
NoteBeing able to reproduce a randomly-generated sequence can be useful. In academic applications, a massive sequence of random values can be generated for a simulation, then reproduced exactly for more detailed analysis later. As another example, in computer games, if a player loads a saved game, any "random" events can be the same as if the game never stopped. That way, the player can't reload the same game repeatedly to try for better luck.
How to generate pseudorandom numbersThe following are some ways you can create a pseudorandom number in common programs and programming LANGUAGES.
Windows Command PromptAt the Windows command prompt, or in a batch file, the special environment variable %RANDOM% produces a pseudorandom number between 0 and 32767, seeded with the time the command prompt launched.
echo "So %RANDOM%!""So 27525!"To create a batch file that generates a random number between 1 and 100:
copy con sorandom.bat echo off set /a myrand=%RANDOM%*100/32768+1 echo The number I was thinking of was %myrand%. Did you get it right?PRESS Ctrl+Z and Enter to save the batch file. Then, execute the file:
sorandomThe number I was thinking of was 91. Did you get it right?Windows PowerShellThe Get-Random cmdlet generates a random number between 0 and 2,147,483,647 (the maximum value of an unsigned 32-bit integer).
Get-Random1333190525The cmdlet takes a number of options, such as a minimum and maximum value. Values are rounded down, so to generate a number between 1 and 100, set the maximum to 101:
Get-Random -Minimum 1 -Maximum 10199Microsoft ExcelIn an Excel spreadsheet, the formula =RAND() generates a random number between 0 and 1. For instance, if you highlight a cell and enter =RAND(), the cell generates a number that changes whenever the sheet is re-calculated.
This method also works in other spreadsheet applications, including LibreOffice Calc and Google Sheets.
Most programming languages have a PRNG functions. Here are some common examples:
CIn the C programming language, the PRNG functions are defined in the standard library, stdlib. The common way to seed the random generator is with the time() function, declared in time.h. The generated number falls between 0 and the constant RAND_MAX, a system-specific integer guaranteed to be at least 32767.
#include <time.h> #include <stdlib.h> #include <stdio.h> void main () { SRAND(time(NULL)); /* seed the generator */ int rand1 = rand(); /* a pseudorandom integer between 0 and RAND_MAX */ printf("Random number between 0 and %d: %d\n", RAND_MAX, (int)rand1); /* Or, within a specific range: */ int min = 0; int max = 100; float rand2 = (float)rand() * max / RAND_MAX + 1; int round = (int)rand2; printf("Random number between %d and %d: %d (%f)\n", min, max, round, rand2); return; }Output:
Random number between 0 and 2147483647: 1789080047 Random number between 0 and 100: 74 (74.369179)C++In C++:
#include <ctime> #include <cstdlib> #include <iostream> int main () { srand(time(NULL)); std::cout << "Random number between 0 and " << RAND_MAX << ": " << rand() << "\n" << "Random number between 1 and 100: " << (rand() % 100) + 1 << std::endl; return 0; }Output:
Random number between 0 and 2147483647: 126569208 Random number between 1 and 100: 9Python 3The random module in Python offers a variety of functions for generating random numbers. In this example, we use three different methods for finding a random integer in a range.
import random from datetime import datetime random.seed(datetime.now()) print("Random number in range [0,1): ", random.random()) # Within a range. These all do the same thing: print("Random number between 1 and 100: ", round(random.random() * 100) + 1) print("Random number between 1 and 100: ", random.randrange(1, 101)) print("Random number between 1 and 100: ", random.randint(1, 100))Output:
Random number in range [0,1): 0.05137418896158319 Random number between 1 and 100: 27 Random number between 1 and 100: 80 Random number between 1 and 100: 80Perl 5In Perl:
srand(time); # changes once per second print "Random number in range [0,1): ", rand(), "\n"; print "Random number in range [1, 100]: ", int(rand(101)), "\n";Output:
Random number in range [0,1): 0.691379946963028 Random number in range [0, 100]: 82JavaScriptconsole.log("Random number in range [0,1): " + Math.random()); console.log("Random number in range [1,100]: " + Math.floor(Math.random() * 101));View the output in your web browser's JavaScript console (for instance, in Firefox press Ctrl+Shift+K):
Random number in range [0,1): 0.305008216755414 Random number in range [1,100]: 8Note:It's not possible to seed the Math.random() function in JavaScript. If you need a robust PRNG in JavaScript, check out better random numbers for JavaScript on GitHub.
Example PRNG: JavaScript widgetUsing the widget below, you can seed a PRNG and use it to generate random numbers.
Each time you generate a random number from your given seed, its offset increases by 1. The first number generated from the seed has offset zero, the second has offset 1, etc. The generator always produces the same number for a given seed and offset.
Enter anything you want into the field to create a unique seed.
Use the Generate button to get the next random number using that seed, and increment the offset.
Use the Reset button to reset the offset to zero.
Input a custom seed (a number or phrase):
Generate! number generated from seed :
Reset seed offset (currently 0)
This widget uses Johannes Baagøe's open source PRNG scripts, Alea.js and Mash.js.
Posted on 20 May 2022, this text provides information on Computer Definitions related to P - Definitions in Computer Definitions. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.
Turn Your Knowledge into Earnings.
No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.