473,408 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Table Random

nurulshidanoni
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++?
Oct 3 '07 #1
12 2756
Studlyami
464 Expert 256MB
i would put that table into an array and then determine a random number between 0 and the size of the array.
Oct 3 '07 #2
how to make into array?


i would put that table into an array and then determine a random number between 0 and the size of the array.
Oct 3 '07 #3
Ganon11
3,652 Expert 2GB
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?
Oct 3 '07 #4
Like below, how to choose random nums?one only.




Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <time.h>
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include <stdlib.h> 
  6. using namespace std;
  7.  
  8.   int main()
  9. {
  10.      int i;
  11.      int nums[66];
  12.  
  13.  
  14.      nums[11]=3;
  15.      nums[12]=6;
  16.      nums[13]=11;
  17.      nums[14]=18;
  18.      nums[15]=27;
  19.      nums[16]=38;
  20.      nums[21]=6;
  21.      nums[22]=9;
  22.      nums[23]=28;
  23.      nums[24]=65; 
  24.      nums[25]=126;
  25.      nums[26]=217; 
  26.      nums[31]=1;
  27.      nums[32]=7; 
  28.      nums[33]=17;
  29.      nums[34]=31; 
  30.      nums[35]=49; 
  31.      nums[36]=71;
  32.      nums[41]=3;
  33.      nums[42]=24;
  34.      nums[43]=81; 
  35.      nums[45]=375;
  36.      nums[44]=192;
  37.      nums[46]=648;
  38.      nums[51]=2;
  39.      nums[52]=52;
  40.      nums[53]=10; 
  41.      nums[54]=17; 
  42.      nums[55]=26; 
  43.      nums[56]=37;
  44.      nums[61]=4;
  45.      nums[62]=7; 
  46.      nums[63]=12; 
  47.      nums[64]=19;
  48.      nums[65]=28;
  49.      nums[66]=39;
  50.  
  51. //choose the random number from data array
  52.     printf("Choose Random Number From Nums\n");













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?
Oct 3 '07 #5
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..
Oct 3 '07 #6
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;
}
Oct 3 '07 #7
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.
Oct 3 '07 #8
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?


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.
Oct 3 '07 #9
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <time.h>
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include <stdlib.h> 
  6. #define WIDTH 7
  7. #define HEIGHT 7
  8.  {   
  9.     int main()
  10.     int i, j;
  11.     int [i][j]=[6][6];
  12.     [i][j]=(2+pow(x[i],2));
  13.     [i][j]=(1+pow(x[i],3));
  14.     [i][j]=(2*pow(x[i],2)-1);
  15.     [i][j]=(3*pow(x[i],3));
  16.     [i][j]=(1+pow(x[i],2));
  17.     [i][j]=(3+pow(x[i],2));
  18.  
  19. {
  20.     for (i=0;j<HEIGHT;i++)
  21.     for (j=0;j<WIDTH;j++)
  22.  
  23.     printf("\n Data for Cost Function and Quantity of Resource Allotted\n");
  24. {
  25.     for(i=1; i<=6; i++)
  26.     for(j=1; j<=6; j++)
  27.  
  28.     [i][j]=[6][6];
  29.     [i][j]=(2+pow(x[i],2));
  30.     [i][j]=(1+pow(x[i],3));
  31.     [i][j]=(2*pow(x[i],2)-1);
  32.     [i][j]=(3*pow(x[i],3));
  33.     [i][j]=(1+pow(x[i],2));
  34.     [i][j]=(3+pow(x[i],2));
  35.  
  36. }   printf("\n");
  37.     for(i=1; i<=6; i++)
  38.     for(j=1; j<=6; j++)
  39. {
  40.  
  41.     printf  ("i\t [i][j]\t [i][j]\t [i][j]\t [i][j]\t\ [i][j]\t [i][j]\n");
  42.     printf  ("-\t -\t -\t -\t -\t -\t -\t\n");     
  43.  
  44. {
  45.     for(i=1; i<=6; i++)
  46.     for(j=1; j<=6; j++)
  47.     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]);
  48.  
  49. }
  50.  
  51. //choose the random number from data array
  52.  
  53.     printf ("Random initial solution: %d\n", rand() % 6);
  54.  
  55.     srand ( 1 );
  56.  
  57.  
  58.     return 0;
  59. }
  60.  
  61. }  }
Oct 3 '07 #10
hariharanmca
1,977 1GB
Do not simply post code.
Explain your problem detail.
Oct 3 '07 #11
Savage
1,764 Expert 1GB
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:

Expand|Select|Wrap|Line Numbers
  1. a[52/*or any other number you get*/]
,regards

Savage
Oct 3 '07 #12
sicarie
4,677 Expert Mod 4TB
nurulshidanoni-

Please confine your questions on the same topic to a single thread.
Oct 3 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Tony Stephens | last post by:
I have a database table that contains a set of numbers (set X). I need to be able to generate/export a random set of survey numbers (set C) from set A with IDs that aren't in set B. Where set A is...
1
by: Terry Haufler | last post by:
I am trying to swap/rotate random flash banners using the following Javascript code. I have 3 flash headers/banners. I can get it to open a page with a random header using...
2
by: IceCube | last post by:
Hello, I would like to select/filter at random 30 records out of an Access-table of 1500 records. I know the option "Top" which gives me the possibility to see the 30 first records of the table...
2
by: Arnau Rebassa | last post by:
Hi everybody, I'm doing the following query: select * from messages order by random() limit 1; in the table messages I have more than 200 messages and a lot of times, the message retrieved...
139
by: ravi | last post by:
Hi can anybody tell me that which ds will be best suited to implement a hash table in C/C++ thanx. in advanced
4
by: Breana | last post by:
Ok, i want to make a sql statment select from a table, random quotes and display a random one each time the page is loaded! I got it to connect, but it keeps selecting all of them... <?php ...
24
by: pereges | last post by:
I need to generate two uniform random numbers between 0 and 1 in C ? How to do it ? I looked into rand function where you need to #define RAND_MAX as 1 but will this rand function give me ...
2
by: blaine | last post by:
Hey everyone, Just a friendly question about an efficient way to do this. I have a graph with nodes and edges (networkx is am amazing library, check it out!). I also have a lookup table with...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.