Random number generator
Jump to navigation
Jump to search
- Not to be confused with XKCD Random Number.
A random number generator (a.k.a RNG) generates a number randomly, usually within some range or subset. It is as the name implies.
This system is often used by "Guessing number".
Python
from random import *
a = input("Please Input how many life do you have(If you guessing wrong, your life will be leaves 1): ")
if a == '':
a = 10
elif a == 'H':
a = 1
a = int(a)
answer = randint(-50, 50)
# randint(X, Y) generates a random integer from X to Y.
while a > 0:
b = input("Please Input what number are you guessing: ")
b = int(b)
if b == answer:
print("That's it! Thanks for playing!")
break
elif b > answer:
a -= 1
print(f"{b} is too big! Try again({a} lifes left): ")
elif b < answer:
a -= 1
print(f"{b} is too small! Try again({a} lifes left): ")
C++
#include<bits/advc++.h>
using namespace std;
int a, b;
string com;
int main()
{
srand((unsigned long)time(NULL));
// When you wanna use rand() function, you must use srand(unsigned long int) to generate a seed.
cout<<"Boot manager"<<endl
<<"1. Normally boot"<<endl
<<"2. Weirdly boot"<<endl
<<"3. Exit"<<endl;
cin>>a;
switch (a)
{
case 1:
{
system("cls");
print("\n\n\n\n\n\n\n\n\n\nLoading Operating System...");
Sleep(5000);
break;
}
case 2:
{
system("cls");
print("\n\n\n\n\n\n\n\n\n\nLoading Operating System...");
Sleep(5000);
for(int i=1;i<=100;i++)
{
for(int j=1;j<=100;j++)
{
cout<<(char)(rand()%127);
}
cout<<"\n"<<endl;
}
Sleep(5000);
system("cls");
break;
}
case 3:
{
print("Exiting...");
Sleep(2000);
system("cls");
return 0;
}
}
system("cls");
system("color 1f");
cout<<"Are you sure to go into OS?"<<endl;
cin>>b;
if(b==0)
{
system("cls");
cout<<"(ノ`Д)ノF*ck you"<<endl;
Sleep(2000);
system("cls");
return 0;
}
system("cls");
cout<<"Genshin Impact(Not) DOS, start!!!!!!!!!!!!!"<<endl;
for(int i=1;i<=20;i++)
{
cout<<"_";
Sleep((rand()%1000)+1);
/* Sleep(int X) function is from windows.h library. It pauses program for X milliseconds.
* rand(void) function generates a random number from 0 to 2147483647.
*/
}
system("cls");
cout<<"MS-DOS 12.3.3222 Windows-based startuping......"<<endl;
Sleep(1000);
system("cls");
for(int i=1;i<=100;i++)
{
for(int j=1;j<=100;j++)
{
cout<<(char)(rand()%127);
// Here generates some random ASCII character.
}
cout<<"\n"<<endl;
}
Sleep(2000);
system("cls");
system("color 0f");
system("pause");
system("cls");
print("Hello, DOS!\n");
print("This OS is based on Microsoft Windows, is a simulator.\n");
Sleep(2000);
system("cls");
while(true)
{
cout<<">>>";
getline(cin, com);
system(com.c_str());
puts("\n\n");
}
}