Question:
how to create array in java and random it?
krisha b
2009-08-25 03:02:15 UTC
i need to produce random questions in java..so we deal with creating string arrays and how can we randomize the indices? thanks much! :)
Three answers:
AnalProgrammer
2009-08-25 05:11:11 UTC
Say you have an array of 10 question. Well 10 Strings because I do not know what you are storing in those Strings.

Create another array of 10 Strings. This is where you are going to create the randomised Strings.



Using a for loop step through the original array.

For each entry you will create a random number between 0 and 9. This will be the new place that the String will go to in the new array.

So if the first random number is 5 and the second random number is 5 also then you need to make sure that you get another random number.

That means that you must keep getting random numbers until the destination String is not null. Another loop.



One thing that you will find with this method is that it is quite slow. The more full the destination array becomes the more chance there is of getting a random number that has already been taken.



Another way of doing this is a pseudo random method.

Use the above method to transfer half the array to the destination array.

Now create another loop for the remaining half.

For each String find the next available empty destination slot.



I hope this helps.
2016-11-30 06:47:28 UTC
you do no longer ought to randomize the checklist, in simple terms the order wherein they're picked. till now the pastime starts, generate an array of fifty values, 0 by way of 40 9 and "shuffle" them. Any card pastime shuffling set of rules will do what you %. Then flow for the period of the randomized numbers so as till the top is reached. you will in no way have a repeat in simple terms like a shuffled deck of playing cards.
qIes.mF8wVaBQ
2009-08-25 03:12:01 UTC
1. Learn to ask better questions.

2. Create an array.

3. Access it in a random way.



Where exactly is your problem?



1. http://catb.org/~esr/faqs/smart-questions.html

2. String[] strings = new String[2];

3. int index = (int) (Math.random() * strings.size());


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...