Table Random

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nurulshidanoni
    New Member
    • Sep 2007
    • 40

    Table Random

    Dear all,

    Given a table 3 | 2 | 1 | 3 | 2 | 4 |
    6 | 9 | 7 | 24 | 5 | 7 |
    11 | 28 | 17 | 81 | 10 | 12 |
    18 | 65 | 31 | 192 | 17 | 19 |
    27 | 126 | 49 | 375 | 26 | 28 |

    How to choose a random number from the table using c++?
  • Studlyami
    Recognized Expert Contributor
    • Sep 2007
    • 464

    #2
    i would put that table into an array and then determine a random number between 0 and the size of the array.

    Comment

    • nurulshidanoni
      New Member
      • Sep 2007
      • 40

      #3
      how to make into array?


      Originally posted by Studlyami
      i would put that table into an array and then determine a random number between 0 and the size of the array.

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Where is this table stored? In a file? Is it already in a 2D array given to you? Is it to be read from input?

        Comment

        • nurulshidanoni
          New Member
          • Sep 2007
          • 40

          #5
          Like below, how to choose random nums?one only.




          [CODE=cpp]#include <iostream>
          #include <time.h>
          #include <stdio.h>
          #include <math.h>
          #include <stdlib.h>
          using namespace std;

          int main()
          {
          int i;
          int nums[66];


          nums[11]=3;
          nums[12]=6;
          nums[13]=11;
          nums[14]=18;
          nums[15]=27;
          nums[16]=38;
          nums[21]=6;
          nums[22]=9;
          nums[23]=28;
          nums[24]=65;
          nums[25]=126;
          nums[26]=217;
          nums[31]=1;
          nums[32]=7;
          nums[33]=17;
          nums[34]=31;
          nums[35]=49;
          nums[36]=71;
          nums[41]=3;
          nums[42]=24;
          nums[43]=81;
          nums[45]=375;
          nums[44]=192;
          nums[46]=648;
          nums[51]=2;
          nums[52]=52;
          nums[53]=10;
          nums[54]=17;
          nums[55]=26;
          nums[56]=37;
          nums[61]=4;
          nums[62]=7;
          nums[63]=12;
          nums[64]=19;
          nums[65]=28;
          nums[66]=39;

          //choose the random number from data array
          printf("Choose Random Number From Nums\n");[/CODE]














          Originally posted by Ganon11
          Where is this table stored? In a file? Is it already in a 2D array given to you? Is it to be read from input?
          Last edited by Ganon11; Oct 3 '07, 01:09 PM. Reason: Please use the [CODE] tags provided.

          Comment

          • kaioshin00
            New Member
            • Nov 2006
            • 46

            #6
            Here's a link to the rand function documentation

            See if you can understand how it works and try to come up with generating a random number that will index properly into your array.

            Notice the valid indices into your array are 11-16, 21-26, 31-36, 41-46, etc..

            Comment

            • nurulshidanoni
              New Member
              • Sep 2007
              • 40

              #7
              When i have get the random number is 52, then how can i get the value?

              I want like this,nums[52]=52;


              #include <iostream>
              #include <time.h>
              #include <stdio.h>
              #include <math.h>
              #include <stdlib.h>
              using namespace std;

              int main()
              {

              int nums[66];


              nums[11]=3;
              nums[12]=6;
              nums[13]=11;
              nums[14]=18;
              nums[15]=27;
              nums[16]=38;
              nums[21]=6;
              nums[22]=9;
              nums[23]=28;
              nums[24]=65;
              nums[25]=126;
              nums[26]=217;
              nums[31]=1;
              nums[32]=7;
              nums[33]=17;
              nums[34]=31;
              nums[35]=49;
              nums[36]=71;
              nums[41]=3;
              nums[42]=24;
              nums[43]=81;
              nums[45]=375;
              nums[44]=192;
              nums[46]=648;
              nums[51]=2;
              nums[52]=52;
              nums[53]=10;
              nums[54]=17;
              nums[55]=26;
              nums[56]=37;
              nums[61]=4;
              nums[62]=7;
              nums[63]=12;
              nums[64]=19;
              nums[65]=28;
              nums[66]=39;

              //choose the random number from data array

              printf ("Random initial solution: %d\n", rand() % 55+11);
              printf("rand () % 55+1=nums[i] ");
              srand ( 1 );


              return 0;
              }

              Comment

              • kaioshin00
                New Member
                • Nov 2006
                • 46

                #8
                Remember, your valid indices are 11-16, 21-26, 31-36, 41-46, 51-56, and 61-66

                You need to generate a random number that fits in one of these intervals

                There might be several ways to do this, one is as follows:

                Create a random number.
                Mod it by 6, and then add 1.

                This will either give you 1, 2, 3, 4, 5, or 6

                Take this new number and multiply it by 10.

                This will give you 10, 20, 30, 40, 50, or 60.

                Crate a new random number.
                Mod it by 7
                This new random number will either be 0, 1, 2, 3, 4, 5, or 6
                Add this new number to the previous number, which was either 10, 20, 30, 40, 50, or 60

                This will give you a random index into your array.

                Let me know if any of that isn't clear.

                Comment

                • nurulshidanoni
                  New Member
                  • Sep 2007
                  • 40

                  #9
                  Thak you for reply my forum. i have get the random number like [52], but i want the value also.How to call the value?


                  Originally posted by kaioshin00
                  Remember, your valid indices are 11-16, 21-26, 31-36, 41-46, 51-56, and 61-66

                  You need to generate a random number that fits in one of these intervals

                  There might be several ways to do this, one is as follows:

                  Create a random number.
                  Mod it by 6, and then add 1.

                  This will either give you 1, 2, 3, 4, 5, or 6

                  Take this new number and multiply it by 10.

                  This will give you 10, 20, 30, 40, 50, or 60.

                  Crate a new random number.
                  Mod it by 7
                  This new random number will either be 0, 1, 2, 3, 4, 5, or 6
                  Add this new number to the previous number, which was either 10, 20, 30, 40, 50, or 60

                  This will give you a random index into your array.

                  Let me know if any of that isn't clear.

                  Comment

                  • nurulshidanoni
                    New Member
                    • Sep 2007
                    • 40

                    #10
                    array matrix random

                    [code=cpp]#include <iostream>
                    #include <time.h>
                    #include <stdio.h>
                    #include <math.h>
                    #include <stdlib.h>
                    #define WIDTH 7
                    #define HEIGHT 7
                    {
                    int main()
                    int i, j;
                    int [i][j]=[6][6];
                    [i][j]=(2+pow(x[i],2));
                    [i][j]=(1+pow(x[i],3));
                    [i][j]=(2*pow(x[i],2)-1);
                    [i][j]=(3*pow(x[i],3));
                    [i][j]=(1+pow(x[i],2));
                    [i][j]=(3+pow(x[i],2));

                    {
                    for (i=0;j<HEIGHT;i ++)
                    for (j=0;j<WIDTH;j+ +)

                    }
                    printf("\n Data for Cost Function and Quantity of Resource Allotted\n");
                    {
                    for(i=1; i<=6; i++)
                    for(j=1; j<=6; j++)

                    [i][j]=[6][6];
                    [i][j]=(2+pow(x[i],2));
                    [i][j]=(1+pow(x[i],3));
                    [i][j]=(2*pow(x[i],2)-1);
                    [i][j]=(3*pow(x[i],3));
                    [i][j]=(1+pow(x[i],2));
                    [i][j]=(3+pow(x[i],2));

                    } printf("\n");
                    for(i=1; i<=6; i++)
                    for(j=1; j<=6; j++)
                    {

                    printf ("i\t [i][j]\t [i][j]\t [i][j]\t [i][j]\t\ [i][j]\t [i][j]\n");
                    printf ("-\t -\t -\t -\t -\t -\t -\t\n");

                    {
                    for(i=1; i<=6; i++)
                    for(j=1; j<=6; j++)
                    printf("%d\t %d\t %d\t %d\t %d\t %d\t %d\n", i, [i][j], [i][j], [i][j], [i][j], [i][j], [i][j]);

                    }

                    //choose the random number from data array

                    printf ("Random initial solution: %d\n", rand() % 6);

                    srand ( 1 );


                    return 0;
                    }

                    } }[/code]
                    Last edited by sicarie; Oct 3 '07, 01:10 PM. Reason: Code tags

                    Comment

                    • hariharanmca
                      Top Contributor
                      • Dec 2006
                      • 1977

                      #11
                      Do not simply post code.
                      Explain your problem detail.

                      Comment

                      • Savage
                        Recognized Expert Top Contributor
                        • Feb 2007
                        • 1759

                        #12
                        Originally posted by nurulshidanoni
                        Thak you for reply my forum. i have get the random number like [52], but i want the value also.How to call the value?
                        how about:

                        [CODE="cpp"]a[52/*or any other number you get*/][/CODE]

                        ,regards

                        Savage

                        Comment

                        • sicarie
                          Recognized Expert Specialist
                          • Nov 2006
                          • 4677

                          #13
                          nurulshidanoni-

                          Please confine your questions on the same topic to a single thread.

                          Comment

                          Working...