10 random words without repetition [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have 20 words in sql database in android
I want to choose 10 random words without repeating and put it in 10 textview if activity is starting
I find this code but it's for numbers only
ArrayList<Integer> number = new
ArrayList <Integer>();
for (int i = 1; i <= 10; ++i)
number.add(i);
Collections.shuffle(number);

If they are in a SQL database, you can use the query:
select word
from t
order by rand()
limit 10;
With 20 words, this should have quite reasonable performance. But if the number of words grows, performance could be an issue.

If you've got XQuery 3.1 you can do
random-number-generator()?permute($words)[position()=1 to 10]

Try this
SELECT TOP 20 WordField
FROM TableName
ORDER By rand()

Related

Permutations of array with at minimum 1 element [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 17 days ago.
Improve this question
I am trying to produce all permutations of an array of size N (i.e. N = 9) with possible elements Z (i.e. Z = [0,1,2,3,4])
Duplicates are allowed, but there needs to be at least one. I am able to write the algorithm to go through all possibilities but the minimum of at least one of each of Z is the part I'm having trouble with.
Looking to do this algorithm in java. Can someone help?

How do I display all numbers that end with the number 3 with a for loop in java? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I need to write a for loop in java that displays all of the numbers ranging from 13 - 93 that end in the number 3. It must include 13 and 93.
Loop from 13 to 93. Increment by 10. Like,
for (int i = 13; i <= 93; i += 10) {
// Print the value
}
If you wanted, you could write a function that achieves this task but with parameters
that allow for more flexible control. Where it takes in arguments representing a
from value and a to value, and something like a endsWith value. It's just a suggestion though.
Any decimal based number mod 10 will get you the last digit.
for (int i = start ; i <= end ; i++) {
if (i % 10 == 3) {
System.out.println(i);
}
}

What can be the algorithm to find the max number that appear the most in an array? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to find out an algorithm that returns the number that has more occurrences in an array that contains number from 0 to 9 and that has a complexity of n.
I though to using an HashMap but it would require n^2
If anyone can write the code down,i'd prefer in Java but pseudocode is the same
Use ten counters (one per digit), scan the array and increment the counters corresponding to the digits. (You are actually computing the histogram of the digit frequencies.)
Report the digit with the largest counter.

How to randomly select one from two integers? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
So lets say I want to randomly choose from the set of two numbers 1 and 3.
How do I go about doing that? Do I just assign int a to 1, int b to 3, and then do randomly select from a and b?
If there are only two numbers to choose from then you can use the value of a boolean, because it returns either true or false.
One-liner solution assuming that int a = 1 and int b = 3:
int randomOfTwoInts = new Random().nextBoolean() ? a : b;
If you have a specific list of numbers then put them in list structure (an array works well). Then you have the easier task of looking for a random index in the range from 0 to last array index. This post lists strategies for doing that:How do I generate random integers within a specific range in Java?

How to use only the last bit provided by this for loop? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So, I'm trying to calculate a list of number to eventually sort, so I only want the final result of this for loop.
for (int anno=startyear; TimePeriod>=anno;anno++) {
System.out.println(anno);
}
Where anno = 1995 and I am counting to the current day, I end up getting a result that slowly counts up, where it first counts at 1995, then it counts 1995 and then 1996, and so on.
How do I only get the end result for use in my program? The result that would simply be 1995-2014. Not the repeats.
edit: Forgot to mention I need every number in between 1995-2014 as well
You shouldn't need a loop for this, assuming your TimePeriod variable equals 2014 then just do the following to print out the desired result:
System.out.println(startyear+"-"+TimePeriod);
That will print out:
1995-2014
You already know the final value: it's TimePeriod. If that's all you need, just use that and get rid of the loop:
System.out.printf("%d-%d", startyear, TimePeriod);

Categories

Resources