How to Fill an Array With Random Values from a Range

C: filling an array with random numbers in a range

As mentioned in comments , if you want number in range 1 to 10 :

array0[i]= rand()%10 + 1;

I suggest int array1[10]={0}; instead of this loop:

for(int i = 0 ; i < 10 ; i++)
{
array1[i]= 0;
}

and here is complete code with printing:

int main()
{
int n;
printf("How many elements in array?:");
scanf("%d",&n);
int array0[n];
for(int i = 0 ; i < n ; i++)
{
array0[i]= rand()%10 + 1;//for your range
}

int array1[10]={0};

/*for(int i = 0 ; i < 10 ; i++)
{
array1[i]= 0;
}*/
int index;
for(int i = 0 ; i < n ; i++)
{
index = array0[i];
array1[index-1]++;
}
for (int i = 0; i < 10; i++)
{
printf("number %d appears:%d\n", i + 1, array1[i]);
}
}

also as @Ardent Coder said add srand(time(NULL)); bfeore rand() to generate different random numbers at different runtimes.

Fill array with random numbers within a specified range (C++)

You are using exact same integer in each initialization. for loop should be like that

    for(int i = 0; i < size; i++){

arr[i] = (rand() % 10);
}

Filling an array with random numbers in a range in C

Georgi.

I've made a successfull test with the following code.
I think the problem could be a several things, but mainly allocating memory locally instead globally.
If this won't help you, I suggest you put here the complete code for your attempt application.

:)

void randomgen(int *intarray, int size, int max, int min)
{
int i;

for (i = 0; i < size; i++) {
intarray[i] = (rand() % (max - min + 1)) + min;
printf("%d ", intarray[i]);
}
}
int main(int argc, char **argv){
int *iA;
int i;

if ( (iA = (int*)malloc(20 * sizeof(int))) == NULL ){
printf("error");
return 0;
}
randomgen(iA, 20, 59, 50);

for (i = 0; i < 20; i++) {
printf("iA[%d]=%d\n", i, iA[i]);
}
return 0;
}

It produces the following output:

53 56 57 55 53 55 56 52 59 51 52 57 50 59 53 56 50 56 52 56
iA[0]=53
iA[1]=56
iA[2]=57
iA[3]=55
iA[4]=53
iA[5]=55
iA[6]=56
iA[7]=52
iA[8]=59
iA[9]=51
iA[10]=52
iA[11]=57
iA[12]=50
iA[13]=59
iA[14]=53
iA[15]=56
iA[16]=50
iA[17]=56
iA[18]=52
iA[19]=56

Populate an array with random numbers in the range of 0-4 with an increment of 0.5

Based on your comments:

double[] numbers = new double[10];
Random random = new Random();
for (int i = 0; i < 10; i++) {
numbers[i] = 0.5 + (random.nextInt(8)) * 0.5;
System.out.println(numbers[i]);
}

random.nextInt(8) picks an integer from [0, 8)... which is then multiplied by 0.5... this is how you get numbers from [0, 4). Later you add 0.5, which gives you a number from [0.5, 4.5).
You can repeat this 10 times to fill your array.

Java: Using a for-loop to fill in an array with random integers within a specific range [duplicate]

Random r = new Random();
int Low = 10;
int High = 100;

for(int i = 0; i < MyArray .length; i++){
MyArray [i] = r.nextInt(High-Low) + Low;
}

Fill an array with random numbers [duplicate]

You need to add logic to assign random values to double[] array using randomFill method.

Change

 public static double[] list(){
anArray = new double[10];
return anArray;
}

To

 public static double[] list() {
anArray = new double[10];
for(int i=0;i<anArray.length;i++)
{
anArray[i] = randomFill();
}
return anArray;
}

Then you can call methods, including list() and print() in main method to generate random double values and print the double[] array in console.

 public static void main(String args[]) {

list();
print();
}

One result is as follows:

-2.89783865E8 
1.605018025E9
-1.55668528E9
-1.589135498E9
-6.33159518E8
-1.038278095E9
-4.2632203E8
1.310182951E9
1.350639892E9
6.7543543E7


Related Topics



Leave a reply



Submit