Ad
How Do I Generate Random String Array Urls Without Repeating?
I am trying to generate random string array urls in picasso, everything is working fine but it repeats, like i had 28 string array items when i start app some items are repeating but i want only 1 item at one time when random start
This is my code
ImageView imageView = itemView.findViewById(R.id.imageview);
random = new Random();
int p= random.nextInt(icons.length);
Picasso.get().load(icons[p]).into(imageView);
Ad
Answer
You can keep track of previously generated integers in an array/list and check the array each time you generate a new random number. This way, if the new integer generated already exists in the array, you generate a new one, until you have generated 28 numbers, after which you will have to clear the array and start over.
ImageView imageView = itemView.findViewById(R.id.imageview);
Random random = new Random();
List<Integer> prevInts = new ArrayList<>();
Picasso.get().load(icons[randomUniqueInteger()]).into(imageView);
public int randomUniqueInteger(){
int p = 0;
do {
p = random.nextInt(icons.length);
} while(prevInts.contains(p));
if ((prevInts.size + 1) == icons.length){
prevInts.clear();
}
prevInts.add(p);
return p;
}
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad