logo
down
shadow

Randomizing strings without duplication


Randomizing strings without duplication

By : Anton
Date : November 19 2020, 12:41 AM
it helps some times Here's one approach that uses an ArayList simulate "picking out of hat" kind of thing you want to do. ArrayList is convenient because you can remove elements easily, and then pick randomly from what's left on the list.
code :
List<String> myStrings = new ArrayList<String>() {{
    add("A");
    add("B");
    add("C");
    add("D");
    add("E");
}};
List<String> dupeList = new ArrayList<String>() {{addAll(myStrings);}};
Random r = new Random();

public String pick() {
    String retval = "";
    int pos;
    switch (dupeList.size()) {
        case 1:
            retval = dupeList.get(0);
            dupeList.clear();
            dupeList.addAll(myStrings);
            return retval;
        default:
            pos = r.nextInt(dupeList.size());
            retval = dupeList.get(pos);
            dupeList.remove(pos);
            return retval;
    }
}
 `text1.setText(pick());`


Share : facebook icon twitter icon
Error On Randomizing Strings In A List C#

Error On Randomizing Strings In A List C#


By : Ado Diba
Date : March 29 2020, 07:55 AM
With these it helps You have to set the listbox selection to reflect the random index that you selected.
Also, if there are no items in the list, you shouldn't do this. So add this code to your method:
code :
if (categories.Count == 0)
    return;
{ "Book Titles", "Movie Titles", "Car Parts", "Human Body Parts", "Transportations" };
{ "Book Titles", "Car Parts", "Human Body Parts", "Transportations" };
List<int> unusedCategories = new List<int> { 0, 1, 2, 3, 4 };
int selectedIndex = rand.Next(unusedCategories.Count);
int index = unusedCategories[selectedIndex];
// at this point, index is the index to one of the items in your `categories` list

switch (index)
{
    ....
}

unusedCategories.RemoveAt(selectedIndex);
if (unusedCategories.Count == 0)
    return;
Aggregate and concatenate strings without duplication

Aggregate and concatenate strings without duplication


By : user3058357
Date : March 29 2020, 07:55 AM
will help you I want to aggregate strings and concatenate them. This is example of what I am using , Replace
code :
FROM   emp
FROM (SELECT DISTINCT deptno, ename FROM  emp)
Randomizing an array of C strings

Randomizing an array of C strings


By : user3095026
Date : March 29 2020, 07:55 AM
Hope this helps you need to make a copy of the generated string. and you need a string terminator
code :
void rand(const char *tab[], int n){
    int i, j;
    char c[10];
    for(i=0; i<n; i++){
        for(j=0; j<9; j++){
            c[j]= rand()%26 + 97;
        }
      c[9] = '\0';
        tab[i]=strdup(c) <<<<=====
    }
}
for(int i=0; i < NO_STRINGS; i++)
{
   free(tab[i]);
}
Concatenating strings from one file to another, without duplication

Concatenating strings from one file to another, without duplication


By : Gokul Natarajan
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , There are two text files; fileA and fileB. , Does this work for you?
code :
$ cat fileA fileB | sort -u
#! /bin/sh
cat fileB | while read l; do
  ! grep -Fxq "$l" fileA && echo "$l" >> fileA
done
Removing character duplication in a vector of strings

Removing character duplication in a vector of strings


By : Putra Krisnayana
Date : March 29 2020, 07:55 AM
this will help I have a list of list of file names like: , Its a single number 126412347815 (and unique too)
code :
result.push_back(accumulate(a.begin(), a.end(), string("")));
#include<iostream>
#include<numeric>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;

template<typename T, typename... Args>
void push_back_vec(std::vector<T>& v, Args&&... args)
{
    (v.push_back(args), ...);
}

int main()
{

    vector<string>x;
    push_back_vec(x, "123", "452", "321");

    string a = accumulate(x.begin(), x.end(), string(""));

    sort(a.begin(), a.end());
    auto end = std::unique(a.begin(), a.end());
    a.erase(end,a.end());
    std::cout << std::string(a.begin(), end) << '\n';

}

Output

12345
Program ended with exit code: 0
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org