How to Populate an Array with Random Numbers

Fill an array with random numbers

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

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);
}

How to fill an array with random numbers from 0 to 99 using the class Math?

It should be like

 ar1[i] = (int)(Math.random() * 100);

When you cast, cast type should be in brackets e.g. (cast type)value

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.

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.

Create an array with random values

Here's a solution that shuffles a list of unique numbers (no repeats, ever).

for (var a=[],i=0;i<40;++i) a[i]=i;

// http://stackoverflow.com/questions/962802#962890
function shuffle(array) {
var tmp, current, top = array.length;
if(top) while(--top) {
current = Math.floor(Math.random() * (top + 1));
tmp = array[current];
array[current] = array[top];
array[top] = tmp;
}
return array;
}

a = shuffle(a);

If you want to allow repeated values (which is not what the OP wanted) then look elsewhere. :)

Need help getting populating array with random numbers 1-10 without using 0 in Java

int random = r.nextInt(10);

would give you a pseudo-random int between 0 and 9. Just add 1 to get a range between 1 and 10 :

int random = r.nextInt(10) + 1;

You must also adjust your handling of the occurrences array to account for the fact that array indices start at 0 :

     int[] occurrences = new int[10];
for (int b : array) {
occurrences[b-1]++;
}

for (int i = 0; i < occurences.length; i++) {
System.out.println(i+1 + " appeared " + occurrences[i] + " times");
}

How do I populate an array with random numbers?

You can pass a range to rand()

Array.new(4) { rand(1...9) }


Related Topics



Leave a reply



Submit