[a / b / c / d / e / f / g / gif / h / hr / k / m / o / p / s / t / u / v / vg / vm / vmg / vr / vrpg / vst / w / wg] [i / ic] [r9k / s4s / vip] [cm / hm / lgbt / y] [3 / aco / adv / an / bant / biz / cgl / ck / co / diy / fa / fit / gd / hc / his / int / jp / lit / mlp / mu / n / news / out / po / pol / pw / qst / sci / soc / sp / tg / toy / trv / tv / vp / vt / wsg / wsr / x / xs] [Settings] [Search] [Mobile] [Home]
Board
Settings Mobile Home
/g/ - Technology


Thread archived.
You cannot reply anymore.


[Advertise on 4chan]


File: pepe sitting at a desk.jpg (137 KB, 1024x1024)
137 KB JPG
// A number guessing game (Attmept 1 "Copyed from claude")
#include <iostream>
// input output
#include <cstdlib>
// for the random function
#include <ctime>
// imports time (for the seed)

int main(){
// start
srand(time(0));
// seeds from time (All random numbers have a seed like how every minecraft world has a seed)
int secret = rand() % 100 + 1;
// I have NO IDEA what this dose. int secret rendom then ??? +1?
int guess = 0;
int attempts = 0;
// Guesses and attempts
std::cout<<"I'm thinking of a number between 1 to 100" <<std::endl;
// print
while(guess != secret){
// while guess is NOT = secret
std::cout<<"enter your guess: ";
// print statement
std::cin>>guess;
// takes into guess
attempts++;
// increease attempts by one?
if(guess < secret){
std::cout<<"Too low!";
// screemes too low
}
else if(guess > secret){
std::cout<<"Too high!";
// same but with high
}
else{
std::cout<<"correct you got that in: "<<attempts << " tryes." <<std::endl;
// fancy success message
}
}
return 0;
// end
}
>>
>srand(time(0));

You're just doing C, also on a system with a broken clock your program is predictable and vulnerable.
>>
dumb frogposter
>>
>>109255611
which system has a broken clock? and how is a number guessing game "vulnerable"
>>
>>109255642
A pirate can break your clock and make all your systems relying on it vulnerable and steal all your SSL secrets
>>
int secret = rand() % 100 + 1;
// I have NO IDEA what this dose. int secret rendom then ??? +1?


You are computing, very poorly, a number between 1 and 100. Except you do NOT HAVE UNIFORM PROBABILITY. Calling rand() will sample the pseudorandom number generator that you seeded, and return a value between 0 and RAND_MAX, which will most likely be INT_MAX, which will most likely be 2,147,483,647. You are then calculating that number modulo 100. Which is to say, divide by 100, take the remainder. This gives a value between 0 and 99, to which you add 1.

Problem: assuming that rand() actually did have the ability to uniformly produce 2,147,483,648 different values with equal probability, using this modulus trick is only effective if the modulus evenly divides the number of possible values. That is to say, it should be a power of 2. Does 2,147,483,648 evenly divide by 100? No. So if I were to iterate over the numbers 0 to 2,147,483,647, calculate that number mod 100, do I print each of the numbers 0 to 99 the same number of times? Fucking no you don't. You have a bias.

So what you should be doing is using C++'s native random number generation library instead of C's. Now go read this example code and try again
https://en.cppreference.com/cpp/numeric/random/uniform_int_distribution#Example
>>
>>109255589
anon use the damn code tags

like this
>>
>>109255894
nvm, I just realize readchan doesn't support them.
>>
>>109255589
>learn C++
warning to not sink into type masturbation too hard, its pointless busywork
>>
>>109255779
>here is your bias bro
 0 - 47 occur each 21474837 times
47 - 99 occur each 21474836 times

probably fine for a number guessing game
>>
File: 1781735375562708.png (346 KB, 626x351)
346 KB PNG
>>109255779
There is an answer here, but I have yet to decipher it
>>
while (OP.hasACock() && OP.isAFaget())
OP.cutOffBenis();
else
OP.anHero();



[Advertise on 4chan]

Delete Post: [File Only] Style:
[Disable Mobile View / Use Desktop Site]

[Enable Mobile View / Use Mobile Site]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.