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++?
12 2756
i would put that table into an array and then determine a random number between 0 and the size of the array.
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.
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?
Like below, how to choose random nums?one only. - #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");
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?
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..
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;
}
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.
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.
- #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;
-
}
-
-
} }
Do not simply post code.
Explain your problem detail.
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 : - a[52/*or any other number you get*/]
,regards
Savage
nurulshidanoni-
Please confine your questions on the same topic to a single thread.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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
|
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 ...
|
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 ...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |