Create an Array with Random Values

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. :)

Create an Array with random numbers

Streams

As @saka1029 commented, the easiest (less code) way is by creating a IntStream, a stream of int values, produced by a call to Random.ints.

int[] sortedRandoms = 
new Random() // Access a random-number generator.
.ints(256, 1, 256 + 1) // Generate a stream of int values, an `IntStream` object.
.sorted() // Sort those generated `int` values.
.toArray(); // Return the generated `int` values in an array.

here I added sorting as well.

How to create a array with n random integers?

You can take input from the user by using a scanner like this -

Scanner input= new Scanner(System.in);
System.out.println("Enter the array size: ");
int n = input.nextInt();

Now create a function generateRandomArray(int n) like this -

public List<Integer> generateRandomArray(int n){
ArrayList<Integer> list = new ArrayList<Integer>(n);
Random random = new Random();

for (int i = 0; i < n; i++)
{
list.add(random.nextInt(1000));
}
return list;
}

Here - random.nextInt(1000) will generate a random number from the range 0 to 1000. You can fix the range as you want.

Now you can call the function with the value n get from the user -

ArrayList<Integer> list1 = generateRandomArray(n);
ArrayList<Integer> list2 = generateRandomArray(n);

Hope it will help.

Creating array of length n with random numbers in JavaScript

I wonder if it's possible to get the right result with a one-liner...

var randoms = [...Array(4)].map(() => Math.floor(Math.random() * 9));
document.body.innerText = randoms;

How to create an array with random numbers and send it to listener without randomizing it again?

In the constructor you are assigning the random numbers to a local variable arr that is overriding the private attribute arr with the same name

Try changing it to:

Random rd = new Random(); // creating Random object
arr = new int[100];
for (int i = 0; i < arr.length; i++) {
arr[i] = rd.nextInt(); // storing random integers in an array
}

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

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