ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Q: Write a C++ language program to perform the following task.
generate a sample of 1000 pseudo-random sequences in a range of 0-000000 to 1.000000 using multiplication congruential method.
The equation used in this method is :
Xi+1= 125Xi (mod m)
where m is a positive numbers. the equation reads using the last number generated in the sequences ,Xi multiply it by 125 , divide the result by m and use the remainder of the division process as Xi+1
yewjingcho, we are not here to do your homework. Please read the forum rules. We can help indirectly, but please do not ask us to directly do your work for you.
If you show what you have so far and ask specific questions then we can help to guide you, but you have to show that you are at least trying.
If you are totally stuck and have no idea where to start, you should talk to your course tutor.
It presents cookbook examples of just about every numerical computing task you could imagine. It is extremely popular, and you can find it in many technical library offerings, get it through inter-library loan, or purchase it at many large bookstore chains.
Here is what the free software people at GNU offer in terms of documentation for the free software package descriptions for random numbers
The first program on this page seems to be very related to what you are doing, but is probably overkill. By following its example, you should probably be able to code a program that at least compiles and runs:
i am not learning programming. but still i have an assignmnet regarding c++. can you help me on this question? still i dont know where are the errors..
Like I'm not learning how to cook but I am cooking.
I have no idea what is an upper limit of the generated reminder but I guess it's either m or the given 125. You should know. Also I did not know how to initialize the x[0]. But that should be enough for you ...
Code:
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
int i;
float z[1001];
int x[1001];
int m = 123;
x[0] = 100234;
for (i = 1; i < 1001; i++) {
x[i] = (125 * x[i-1]) % m;
z[i] = (float)x[i] / (float)m;
}
for (i = 1; i < 1001; i++) {
cout << " z[" << i << "] = " << z[i] << endl;
}
return 0;
}
thanks for help. now i want divide the number generated in the question above into 20 intervals from 0 to 1. i need to calculate the frequency of the number generated in each interval. i used the code below but it doesn't seems to work. why?
int a,b,c,d,e,f,g,h,ii,j,k,l,mm,n,o,p,q,r,s,t
{
if(0<z[i]&&z[i]<0.05)
a++;
if(0.05<z[i]&&z[i]<0.1)
b++;
if(0.1<z[i]&&z[i]<1.5)
c++;
if(0.15<z[i]&&z[i]<2)
d++;
if(0.2<z[i]&&z[i]<2.5)
e++;
if(0.25<z[i]&&z[i]<3)
f++;
if(0.3<z[i]&&z[i]<3.5)
g++;
if(0.35<z[i]&&z[i]<4)
h++;
if(0.4<z[i]&&z[i]<4.5)
ii++;
if(0.45<z[i]&&z[i]<5)
j++;
if(0.5<z[i]&&z[i]<5.5)
k++;
if(0.55<z[i]&&z[i]<6)
l++;
if(0.6<z[i]&&z[i]<6.5)
mm++;
if(0.65<z[i]&&z[i]<7)
n++;
if(0.7<z[i]&&z[i]<7.5)
o++;
if(0.75<z[i]&&z[i]<8)
p++;
if(0.8<z[i]&&z[i]<8.5)
q++;
if(0.85<z[i]&&z[i]<9)
r++;
if(0.9<z[i]&&z[i]<9.5)
s++;
if(0.95<z[i]&&z[i]<1.00)
t++;
cout << a << endl;
cout << b << endl;
cout << c << endl;
cout << d << endl;
cout << e << endl;
cout << f << endl;
cout << g << endl;
cout << h << endl;
cout << ii << endl;
cout << j << endl;
cout << k << endl;
cout << l << endl;
cout << mm << endl;
cout << n << endl;
cout << o << endl;
cout << p << endl;
cout << q << endl;
cout << r << endl;
cout << s << endl;
cout << t << endl;
}
Please look again at the structure of that simple programs that we posted before. Where did you put your new piece of code? Where is 'i' variable coming from in your code? You don't need to have all that alphabet of variables. Use another array and the loop. In the for loop just increase some variable by 0.5.
And please hit the pound symbol (#) after highlighting your code so that it will insert [ CODE ] [ /CODE ] around the actual code.
Can I have a big sack of #, erm no make that pounds £'s lol
I personally just use the very old method of typing two tags, I didn't even know this board had a feature like this.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.