Jump to content

(C++)Any way to limit the number of digits in a variable?

FakezZ

So I made a program to generate "random" numbers, but the numbers generated are friggin big! Is there any way to limit the number of digits (I don't wanna use rand(), because I want to do it myself :P)?

 

 

code:

 

#include <iostream>
#include <cmath>
int main()
{
int a = 32840;
int b = 299792458;
int d = 32677;
int s;
for (int x =1; x<=165400; x++)
{
s = (a*b + d) % 65689;
d=s;
std::cout << s << std::endl;
}
int n;
system ("PAUSE");
}

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

well show us what you got..

lol yeah I copied the code, but forgot to paste it xD

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

Well assuming your random number generator creates evenly distributed number, you could always just do %[maxnumber] to get values between 0 and [maxnumber]-1

 

Based on your formula, I am guessing you won't get too random of a number...but that is the concept behind getting the random numbers below a certain number.

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

Well assuming your random number generator creates evenly distributed number, you could always just do %[maxnumber] to get values between 0 and [maxnumber]-1

 

Based on your formula, I am guessing you won't get too random of a number...but that is the concept behind getting the random numbers below a certain number.

Excuse me if I didn't understand correctly, but you are suggesting to do a cyclic division with the max number I want? If that is the case, then I would prefer generating huge numbers, but only use the first lets say 10 digits and leave the rest. Is there any way to do that?

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

Well I mean using module (%) to get the range of numbers.

 

e.g. if you are generating numbers "randomly" between 0 and 4,294,967,295 (size of unsigned int), but you only want 0 to 999 you would simply go

int newValue = oldValue % 1000;

 

range 0 - 24

int newValue = oldValue % 25;

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

Well I mean using module (%) to get the range of numbers.

 

e.g. if you are generating numbers "randomly" between 0 and 4,294,967,295 (size of unsigned int), but you only want 0 to 999 you would simply go

int newValue = oldValue % 1000;

 

range 0 - 24

int newValue = oldValue % 25;

Okay now I get it, but when i multiply a number by a really big one I get negative values for some reason O.o

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

Okay now I get it, but when i multiply a number by a really big one I get negative values for some reason O.o

That is because you are using int type instead of unsigned int type.

 

e.g.

unsigned int s = (unsigned int)-1; //Will produce a very large number :P

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

That is because you are using int type instead of unsigned int type.

 

e.g.

unsigned int s = (unsigned int)-1; //Will produce a very large number :P

Thank you soo much man! You have solved many questions I have been having pretty much since I started programming :)

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

That is because you are using int type instead of unsigned int type.

 

e.g.

unsigned int s = (unsigned int)-1; //Will produce a very large number :P

So I have been experimenting with what you tought me and I end up with this code in order to output the numbers to a txt file :

 

 

#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
int main()
{
unsigned _int64 a = 32840;
unsigned _int64 b = 299792458;
unsigned _int64 d = 32677;
unsigned _int64 s;
for (int x =1; x<=165; x++)
{
s = (a*b + d) % 5477522378482473;
a=sqrt(s);
s=s % 937692130485;
//cout << s << std::endl;
ofstream file;
    file.open ("C:\\x\\serial.txt");
file << s << endl;
file.close();
}
return 0;
 
 
}
 
 
But, it only outputs the first number. What am I doing wrong :S

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

Okay now I get it, but when i multiply a number by a really big one I get negative values for some reason O.o

 

try to understand how data types work ;)

Mini-Desktop: NCASE M1 Build Log
Mini-Server: M350 Build Log

Link to comment
Share on other sites

Link to post
Share on other sites

try to understand how data types work ;)

What do you mean? The problem is that the file only contains the first generated number, but it "should" contain them all :/

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

The lines

ofstream file;    file.open ("C:\\x\\serial.txt");file.close();

should not be inside the for loop...every time you start the program this is what happens.  Initialize the variables, enter your for loop, open file, write to file, close file.  Opening the file overwrites your file...so put those lines outside of the for loop....ie you want your program to initialize the variables, open file, enter for loop, write to file, after the for loop has ended close the file.

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

The lines

ofstream file;    file.open ("C:\\x\\serial.txt");file.close();

should not be inside the for loop...every time you start the program this is what happens.  Initialize the variables, enter your for loop, open file, write to file, close file.  Opening the file overwrites your file...so put those lines outside of the for loop....ie you want your program to initialize the variables, open file, enter for loop, write to file, after the for loop has ended close the file.

Ok thanks :D

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

The lines

ofstream file;    file.open ("C:\\x\\serial.txt");file.close();

should not be inside the for loop...every time you start the program this is what happens.  Initialize the variables, enter your for loop, open file, write to file, close file.  Opening the file overwrites your file...so put those lines outside of the for loop....ie you want your program to initialize the variables, open file, enter for loop, write to file, after the for loop has ended close the file.

aghhh did it and now the text file is empty :S This is confusing...

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

What do you mean? The problem is that the file only contains the first generated number, but it "should" contain them all :/

 

well if you don't know why the number flips to negative, you clearly have a lack of knowledge about how primitive data types work. no offense intended.

Mini-Desktop: NCASE M1 Build Log
Mini-Server: M350 Build Log

Link to comment
Share on other sites

Link to post
Share on other sites

#include <iostream>

#include <cmath>

int main()

{

unsigned long long a = 32840;

unsigned long long b = 299792458;

unsigned long long d = 32677;

unsigned long long s;

for (int x =1; x<=165400; x++)

{

s = (a*b + d) % 65689;

while(s>max value that you want s to be)s%=10;

d=s;

std::cout << s << std::endl;

}

int n;//

system ("PAUSE");

return 0;

}

If you want to make a simple "number generator" you cand use the date and time, so the number will be different every time you use it.

Link to comment
Share on other sites

Link to post
Share on other sites

 

#include <iostream>
#include <cmath>
int main()
{
unsigned long long a = 32840;
unsigned long long b = 299792458;
unsigned long long d = 32677;
unsigned long long s;
for (int x =1; x<=165400; x++)
{
s = (a*b + d) % 65689;
while(s>max value that you want s to be)s%=10;
d=s;
std::cout << s << std::endl;
}
int n;//
system ("PAUSE");
return 0;
}
If you want to make a simple "number generator" you cand use the date and time, so the number will be different every time you use it.

 

Yeah I solved this problem, however now I want to try to output these numbers to a text file to save them :/

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

btw meant to say...code tags when the code is longer people :P

 

Anyways this is just a quick example of how I would format this thing

 #include <iostream>#include <cmath>#include <fstream>using namespace std;int main(){	unsigned _int64 a = 32840;	unsigned _int64 b = 299792458;	unsigned _int64 d = 32677;	unsigned _int64 s;	ofstream file;	file.open("C:\\x\\serial.txt");	for (int x =1; x<=165; x++)	{		s = (a*b + d) % 5477522378482473;		a=sqrt(s);		s=s % 937692130485;		file << s << endl;	}	file.close();	return 0;}

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

  • 2 weeks later...

                                                                                                                                            Praise Duarte!

Link to comment
Share on other sites

Link to post
Share on other sites

  • 4 weeks later...

The main topic seems to have been answered, but I thought I'd point this out.

 

You called:

ofstream file;file.open("C:\\x\\serial.txt");

Which, as someone said, wipes the file out every time you open it.  If you wanted to open the file to append to it, use the following:

ofstream file;file.open("C:\\x\\serial.txt", std::ios::app);

But moving it outside the for loop was the best option in this case.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×