Please help me on my simple program PSEUDO RANDOM NUMBERS

This is a discussion on Please help me on my simple program PSEUDO RANDOM NUMBERS within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <conio.h> int main() { system("TITLE Pseudo Random Numbers"); system("COLOR 2"); char again; int a,c,m,x; int nn0,nn1,nn2,nn3,nn4,nn5,nn6,nn7,nn8,nn9,nn10,nn11,nn12,nn13,nn14,nn15,nn16,nn17,nn18,nn19,nn20; ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    13

    Smile Please help me on my simple program PSEUDO RANDOM NUMBERS

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
    system("TITLE Pseudo Random Numbers");
    system("COLOR 2");
    
    char again;
    int a,c,m,x;
    int nn0,nn1,nn2,nn3,nn4,nn5,nn6,nn7,nn8,nn9,nn10,nn11,nn12,nn13,nn14,nn15,nn16,nn17,nn18,nn19,nn20;
    int n0,n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,n13,n14,n15,n16,n17,n18,n19,n20;
    int b;
    
    do
    {
    system("CLS");
    for(b=0;b<66;b++)
    {
    printf("*");
    }
    
    printf("\n**********************PSEUDO*RANDOM*NUMBERS***************by:jan2\n");
    for(b=0;b<66;b++)
    {
    printf("*");
    }
    printf("\n\n\t FORMULA: Xn + 1( A xn + C ) mod M with Seed Xn = X\n");
    
    
    
    printf("\n Enter the value of A: ");
    scanf("%d", &a);
    printf(" Enter the value of C: ");
    scanf("%d", &c);
    printf(" Enter the value of M: ");
    scanf("%d", &m);
    printf(" Enter the value of X: ");
    scanf("%d", &x);
    
    
    printf("\n\tEQUATION: Xn + 1( %d xn + %d ) mod %d with Seed Xn = %d\n\n\n", a,c,m,x);
    
    
    nn0 = (a*x+c)/m;
    n0 = (a*x+c)-(nn0*m);
    
    nn1 = (a*n0+c)/m;
    n1 = (a*n0+c)-(nn1*m);
    
    nn2 = (a*n1+c)/m;
    n2 = (a*n1+c)-(nn2*m);
    
    nn3 = (a*n2+c)/m;
    n3 = (a*n2+c)-(nn3*m);
    
    nn4 = (a*n3+c)/m;
    n4 = (a*n3+c)-(nn4*m);
    
    nn5 = (a*n4+c)/m;
    n5 = (a*n4+c)-(nn5*m);
    
    nn6 = (a*n5+c)/m;
    n6 = (a*n5+c)-(nn6*m);
    
    nn7 = (a*n6+c)/m;
    n7 = (a*n6+c)-(nn7*m);
    
    nn8 = (a*n7+c)/m;
    n8 = (a*n7+c)-(nn8*m);
    
    nn9 = (a*n8+c)/m;
    n9 = (a*n8+c)-(nn9*m);
    
    nn10 = (a*n9+c)/m;
    n10 = (a*n9+c)-(nn10*m);
    
    nn11 = (a*n10+c)/m;
    n11 = (a*n10+c)-(nn11*m);
    
    nn12 = (a*n11+c)/m;
    n12 = (a*n11+c)-(nn12*m);
    
    nn13 = (a*n12+c)/m;
    n13 = (a*n12+c)-(nn13*m);
    
    nn14 = (a*n13+c)/m;
    n14 = (a*n13+c)-(nn14*m);
    
    nn15 = (a*n14+c)/m;
    n15 = (a*n14+c)-(nn15*m);
    
    nn16 = (a*n15+c)/m;
    n16 = (a*n15+c)-(nn16*m);
    
    nn17 = (a*n16+c)/m;
    n17 = (a*n16+c)-(nn17*m);
    
    nn18 = (a*n17+c)/m;
    n18 = (a*n17+c)-(nn18*m);
    
    nn19 = (a*n18+c)/m;
    n19 = (a*n18+c)-(nn19*m);
    
    nn20 = (a*n19+c)/m;
    n20 = (a*n19+c)-(nn20*m);
    
    printf("\t\t n=0 x1=(%d.%d+%d)mod %d = %d\n",a,x,c,m,n0);
    printf("\t\t n=1 x2=(%d.%d+%d)mod %d = %d\n",a,n0,c,m,n1);
    printf("\t\t n=2 x3=(%d.%d+%d)mod %d = %d\n",a,n1,c,m,n2);
    printf("\t\t n=3 x4=(%d.%d+%d)mod %d = %d\n",a,n2,c,m,n3);
    printf("\t\t n=4 x5=(%d.%d+%d)mod %d = %d\n",a,n3,c,m,n4);
    printf("\t\t n=5 x6=(%d.%d+%d)mod %d = %d\n",a,n4,c,m,n5);
    printf("\t\t n=6 x7=(%d.%d+%d)mod %d = %d\n",a,n5,c,m,n6);
    printf("\t\t n=7 x8=(%d.%d+%d)mod %d = %d\n",a,n7,c,m,n7);
    printf("\t\t n=8 x9=(%d.%d+%d)mod %d = %d\n",a,n8,c,m,n8);
    printf("\t\t n=9 x10=(%d.%d+%d)mod %d = %d\n",a,n9,c,m,n9);
    printf("\t\t n=10 x11=(%d.%d+%d)mod %d = %d\n",a,n10,c,m,n10);
    printf("\t\t n=11 x12=(%d.%d+%d)mod %d = %d\n",a,n11,c,m,n11);
    printf("\t\t n=12 x13=(%d.%d+%d)mod %d = %d\n",a,n12,c,m,n12);
    printf("\t\t n=13 x14=(%d.%d+%d)mod %d = %d\n",a,n13,c,m,n13);
    printf("\t\t n=14 x15=(%d.%d+%d)mod %d = %d\n",a,n14,c,m,n14);
    printf("\t\t n=15 x16=(%d.%d+%d)mod %d = %d\n",a,n15,c,m,n15);
    printf("\t\t n=16 x17=(%d.%d+%d)mod %d = %d\n",a,n16,c,m,n16);
    printf("\t\t n=17 x18=(%d.%d+%d)mod %d = %d\n",a,n17,c,m,n17);
    printf("\t\t n=18 x19=(%d.%d+%d)mod %d = %d\n",a,n18,c,m,n18);
    printf("\t\t n=19 x20=(%d.%d+%d)mod %d = %d\n",a,n19,c,m,n19);
    printf("\t\t n=20 x21=(%d.%d+%d)mod %d = %d\n",a,n20,c,m,n20);
    
    printf("\n\n The numbers generated are:\n %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d%,d,%d,%d,%d,%d,%d,%d\n",n0,n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,n13,n14,n15,n16,n17,n18,n19,n20);
    
    printf("\nWould you like to back to start?? Press \"y\" if YES\n");
    scanf("%s", &again);
    }while (again == 'y');
    
    return 0;
    getch();
    
    }
    >>Please help me..
    What i want to happen is if the output of is repeatedly... the rest of the output will not be shown...
    Ex.
    n=0 x1 = ( 7.6 + 3 ) mod 4 = 1
    n=1 x2 = ( 7.1 + 3 ) mod 4 = 2
    n=2 x3 = ( 7.2 + 3 ) mod 4 = 1 <<<<it will stop here.. the program will stop to show the rest of the output because the output of n=0 and n=2 is the same.. so n=3,n=4 and so on will not be shown...

    Please help me on this.. Thank you in advance. God Bless.

  2. #2
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    You could replace those definitions with 2 arrays and loop through them to initialize them.
    eg.
    Code:
    int i, n[20], nn[20];
    
    nn[0] = (a*x+c)/m;
    n[0] = (a*x+c)-(nn[0]*m);
    
    for(i = 1;i <= 19;i++)
    {
         nn[1] = (a*n[0]+c)/m;
         n[1] = (a*n[0]+c)-(nn[1]*m);
    }

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    13

    Smile

    Quote Originally Posted by User Name: View Post
    You could replace those definitions with 2 arrays and loop through them to initialize them.
    eg.
    Code:
    int i, n[20], nn[20];
    
    nn[0] = (a*x+c)/m;
    n[0] = (a*x+c)-(nn[0]*m);
    
    for(i = 1;i <= 19;i++)
    {
         nn[1] = (a*n[0]+c)/m;
         n[1] = (a*n[0]+c)-(nn[1]*m);
    }
    Wow thanks a lot for that codes .. i love you thank you )
    Im still waiting to the answer in my question.. i love this forum

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    We'll take 4 steps:

    1) assign a value to the final result. The number in red:
    n=0 x1 = ( 7.6 + 3 ) mod 4 = 1
    n=1 x2 = ( 7.1 + 3 ) mod 4 = 2
    n=2 x3 = ( 7.2 + 3 ) mod 4 = 1 <<<<it will stop here.. the program will stop to show the rest of the output because the output of n=0 and n=2 is the same.. so n=3,n=4 and so on will not be shown...
    And assignments always work right to left, of course.

    2) do a distribution count on your data, using a small array:
    Code:
    dcount[SIZE]; //set every element of dcount to zero
    for(i=0;i<SIZE;i++)
      dcount[i]=0;
    3) as each variable in #1 is produced (in a loop), increment it's element in dcount[], by one, like this:
    dcount[Variable]++;

    4) at the bottom of the loop that generates these variables in #1, you'll check the dcount[Variable] to see if it's greater than 1. If it is, then you have a duplicate value. A break statement exits your loop.

    This looks like a big amount of code - but it is not. C is a pretty concise language, and it's best to keep it short, sweet, and clear.

    This is a good trick to have in your programmer's bag of tricks, I've used it in many programs.
    Last edited by Adak; 09-11-2010 at 09:10 AM.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    13
    Quote Originally Posted by Adak View Post
    We'll take 4 steps:

    1) assign a value to the final result. The number in red:


    And assignments always work right to left, of course.

    2) do a distribution count on your data, using a small array:
    Code:
    dcount[SIZE]; //set every element of dcount to zero
    for(i=0;i<SIZE;i++)
      dcount[i]=0;
    3) as each variable in #1 is produced (in a loop), increment it's element in dcount[], by one, like this:
    dcount[Variable]++;

    4) at the bottom of the loop that generates these variables in #1, you'll check the dcount[Variable] to see if it's greater than 1. If it is, then you have a duplicate value. A break statement exits your loop.

    This looks like a big amount of code - but it is not. C is a pretty concise language, and it's best to keep it short, sweet, and clear.

    This is a good trick to have in your programmer's bag of tricks, I've used it in many programs.
    Thank's Sir Adak .. but would u mind if i request you to do the whole codes?? Thank you in advance.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    13
    Code:
    for(i = 1;i <= 19;i++)
    {
         nn[1] = (a*n[0]+c)/m;
         n[1] = (a*n[0]+c)-(nn[1]*m);
    }
    it doesnt increment by 1.. why is it???

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    You must learn to indent.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by metaljacob View Post
    Code:
    for(i = 1;i <= 19;i++)
    {
         nn[1] = (a*n[0]+c)/m;
         n[1] = (a*n[0]+c)-(nn[1]*m);
    }
    it doesnt increment by 1.. why is it???
    Please show us the code where you are defining your variables...

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    2,767
    Quote Originally Posted by metaljacob View Post
    Code:
    for(i = 1;i <= 19;i++)
    {
         nn[1] = (a*n[0]+c)/m;
         n[1] = (a*n[0]+c)-(nn[1]*m);
    }
    it doesnt increment by 1.. why is it???
    "1" does NOT equal "i" or even "l"

    Tim S.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I thought you were going to use rand(), MJ.

    So what's your goal here? Generate 20 unique random numbers? Please post up your latest code, and a good description of just what you want to do? Also, of course, what has you stumped currently.

    I see "eagle eye" Tim has been helpful - well done, Tim.

  11. #11
    Registered User
    Join Date
    Sep 2010
    Posts
    13
    this is my latest codes..

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main(void)
    {
          system("TITLE Pseudo Random Numbers");
          system("COLOR 2");
          
          char again;
          int a,c,m,x;
          int i=1, n[21], nn[21];
          int b;
          
          do
          {
          system("CLS");   
          for(b=0;b<66;b++)
          {    
          printf("*");
          }
            
          printf("\n**********************PSEUDO*RANDOM*NUMBERS***************by:jan2:@\n");
              for(b=0;b<66;b++)
          {    
          printf("*");
          }
          printf("\n\n\t FORMULA: Xn + 1( A xn + C ) mod M with Seed Xn = X\n");
          
                 
         
          printf("\n Enter the value of A: ");
          scanf("%d", &a);
          printf(" Enter the value of C: ");
          scanf("%d", &c);
          printf(" Enter the value of M: ");
          scanf("%d", &m);
          printf(" Enter the value of X: ");
          scanf("%d", &x);
    
          
          printf("\n\tEQUATION: Xn + 1( %d xn + %d ) mod %d with Seed Xn = %d\n\n\n", a,c,m,x);
       
          
          nn[0] = (a*x+c)/m;
          n[0] = (a*x+c)-(nn[0]*m);
          
          for(i = 1;i <= 19;i++)
          {
            nn[1] = (a*n[0]+c)/m;
             n[1] = (a*n[0]+c)-(nn[1]*m);
          }
    
       
          printf("\t\t    n=0 x1=(%d.%d+%d)mod %d = %d\n",a,x,c,m,n[0]);     
          printf("\t\t    n=1 x2=(%d.%d+%d)mod %d = %d\n",a,n[0],c,m,n[1]);
          printf("\t\t    n=2 x3=(%d.%d+%d)mod %d = %d\n",a,n[1],c,m,n[2]);
          printf("\t\t    n=3 x4=(%d.%d+%d)mod %d = %d\n",a,n[2],c,m,n[3]);
          printf("\t\t    n=4 x5=(%d.%d+%d)mod %d = %d\n",a,n[3],c,m,n[4]);
          printf("\t\t    n=5 x6=(%d.%d+%d)mod %d = %d\n",a,n[4],c,m,n[5]);
          printf("\t\t    n=6 x7=(%d.%d+%d)mod %d = %d\n",a,n[5],c,m,n[6]);
          printf("\t\t    n=7 x8=(%d.%d+%d)mod %d = %d\n",a,n[6],c,m,n[7]);
          printf("\t\t    n=8 x9=(%d.%d+%d)mod %d = %d\n",a,n[7],c,m,n[8]);
          printf("\t\t   n=9 x10=(%d.%d+%d)mod %d = %d\n",a,n[8],c,m,n[9]);
          printf("\t\t  n=10 x11=(%d.%d+%d)mod %d = %d\n",a,n[9],c,m,n[10]);
          printf("\t\t  n=11 x12=(%d.%d+%d)mod %d = %d\n",a,n[10],c,m,n[11]);
          printf("\t\t  n=12 x13=(%d.%d+%d)mod %d = %d\n",a,n[11],c,m,n[12]);
          printf("\t\t  n=13 x14=(%d.%d+%d)mod %d = %d\n",a,n[12],c,m,n[13]);
          printf("\t\t  n=14 x15=(%d.%d+%d)mod %d = %d\n",a,n[13],c,m,n[14]);
          printf("\t\t  n=15 x16=(%d.%d+%d)mod %d = %d\n",a,n[14],c,m,n[15]);
          printf("\t\t  n=16 x17=(%d.%d+%d)mod %d = %d\n",a,n[15],c,m,n[16]);
          printf("\t\t  n=17 x18=(%d.%d+%d)mod %d = %d\n",a,n[16],c,m,n[17]);
          printf("\t\t  n=18 x19=(%d.%d+%d)mod %d = %d\n",a,n[17],c,m,n[18]);
          printf("\t\t  n=19 x20=(%d.%d+%d)mod %d = %d\n",a,n[18],c,m,n[19]);
          printf("\t\t  n=20 x21=(%d.%d+%d)mod %d = %d\n",a,n[19],c,m,n[20]);
          
          printf("\n\n   The numbers generated are:\n        %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d%,d,%d,%d,%d,%d,%d,%d\n",n[0],n[1],n[2],n[3],n[4],n[5],n[6],n[7],n[8],n[9],n[10],n[11],n[12],n[13],n[14],n[15],n[16],n[17],n[18],n[19],n[20]);
        
           
        printf("\n\tWould you like to start again?? \n Enter \"y\" if YES or enter anykey to EXIT.\n");
        scanf("%s", &again);
       }while (again == 'y');    
      
      return 0;
    The output for n2,n3 until n20 is incorrect.. i think it doesn't increment by 1..
    Code:
     for(i = 1;i <= 19;i++)
          {
            nn[1] = (a*n[0]+c)/m;
             n[1] = (a*n[0]+c)-(nn[1]*m);
          }
    thanks for the help..

  12. #12
    Registered User
    Join Date
    Sep 2010
    Posts
    13
    Quote Originally Posted by Adak View Post
    I thought you were going to use rand(), MJ.

    So what's your goal here? Generate 20 unique random numbers? Please post up your latest code, and a good description of just what you want to do? Also, of course, what has you stumped currently.

    I see "eagle eye" Tim has been helpful - well done, Tim.

    Actually what i want to do is... the output will stop if the output is happened twice..

    Example:

    n=0( line 1 )= 2
    n=1( line 2 )= 4
    n=2( line 3 )= 1
    n=3( line 4 )= 3
    n=4( line 5 )= 7
    n=5( line 6 )= 2 << it will stop here because the output here(which is "2") is the same in the output for n=0(which is in line 1)

    another examples;
    n=0( line 1 )= 2
    n=1( line 2 )= 4
    n=2( line 3 )= 12
    n=3( line 4 )= 3
    n=4( line 5 )= 7
    n=5( line 6 )= 5
    n=6( line 7 )= 10
    n=7( line 8 )= 6
    n=8( line 9 )= 11
    n=9( line 10 )=42
    n=10( line 11 )= 9
    n=11( line 12 )= 2
    ____________

    n=0( line 1 )= 4
    n=1( line 2 )= 6
    n=2( line 3 )= 4


    I hope you understand what im trying to say.. Thank you

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
     for(i = 1;i <= 19;i++)
          {
            nn[1] = (a*n[0]+c)/m;
             n[1] = (a*n[0]+c)-(nn[1]*m);
          }
    As was mentioned earlier, this loop looks flawed, because n[0], and nn[1], is never being changed.

    Leading to no change happening in n[1].

    Somewhere in the loop, you need to include a change made to your variables in one loop, which will effect the answer provided later in the loop. 99% of the time, the variable i will be a part of that change.

    Do you see what I mean?

    Let's get the calculation loop accurate, I can help with the "no repetitions" part, afterward.
    Last edited by Adak; 09-14-2010 at 05:08 AM.

  14. #14
    Registered User
    Join Date
    Sep 2010
    Posts
    13
    Ok i did this

    Code:
    for(i = 1;i <= 19;i++)
          {
            nn[i] = (a*n[0]+c)/m;
             n[i] = (a*n[0]+c)-(nn[1]*m);
          }
    still incorrect output..

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It may take a bit to get them right. Your original equations had the answers on the far right hand side of the equation - which is (as you know), backward to what C requires. So be patient.

    You are multiplying with int's (which is fine), but dividing with an int will truncate (cut off), any remainder at all. Is that OK, or do you need that remainder?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about random numbers
    By Kempelen in forum C Programming
    Replies: 2
    Last Post: 07-02-2008, 07:28 AM
  2. drand48(): generating pseudo random numbers in a range
    By Isolda_ in forum C Programming
    Replies: 2
    Last Post: 08-31-2007, 12:21 PM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  4. Simple window program
    By baniakjr in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2006, 03:46 PM
  5. Generating Random Numbers
    By FromHolland in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2003, 10:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21