Bash: how to avoid duplicate result from random list?Strange behaviour of uninitialized arrays and unset...

What does Mildred mean by this line in Three Billboards Outside Ebbing, Missouri?

Is it bad to describe a character long after their introduction?

Could human civilization live 150 years in a nuclear-powered aircraft carrier colony without resorting to mass killing/ cannibalism?

What is the line crossing the Pacific Ocean that is shown on maps?

Can 'leave' mean 'forget'?

"Plugged in" or "Plugged in in"

How can I reduce the sound of rain on a range hood vent?

How hard is it to sell a home which is currently mortgaged?

The Confused Alien

Does “comme on était à New York” mean “since” or “as though”?

What is the name of this OOB notification method/popup, and is it customizable?

What is "oversubscription" in Networking?

How would an order of Monks that renounce their names communicate effectively?

Who gets an Apparition licence?

Do space suits measure "methane" levels or other biological gases?

Can a single server be associated with multiple domains?

Wrong corporate name on employment agreement

Why are there so many religions and gods?

Are metaheuristics ever practical for continuous optimization?

Is it allowed to spend a night in the first entry country before moving to the main destination?

Do I have to roll to maintain concentration if a target other than me who is affected by my concentration spell takes damage?

Automatically convert a number to use the correct SI unit prefix

Can you sign using a digital signature itself?

Using aluminium busbar/cables in an aircraft instead of copper



Bash: how to avoid duplicate result from random list?


Strange behaviour of uninitialized arrays and unset arraysDuplicate a file with random probabilityHow to check if reading from /dev/random will blockBatch file rename: Better way than random id to prevent deletion of files with duplicate filenames?Four random words from a list with Perl!Bash - Replacing random number between quotesBash RANDOM with seed?Bash: How to generate random float number using $RANDOMGenerate random number after value in bashCreate unique random numbers (UUIDs) in bash






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







0















Suppose a script like this



#!/bin/bash
array[0]="123"
array[1]="333"
array[2]="5566"
array[3]="135"
array[4]="68"
array[5]="45"
array[6]="78"
array[7]="32"
array[8]="190"

number()
{
size=${#array[@]}
index=$(($RANDOM % $size))
sleep 0.5s
echo Lucky number is ... ${array[$index]}
}

for i in {1..21}; do number; done


Is ok..I want to get random number in the interval,but show also a lot of dups



Lucky number is ... 135
Lucky number is ... 135
Lucky number is ... 5566
Lucky number is ... 78
Lucky number is ... 190
Lucky number is ... 333
Lucky number is ... 190
Lucky number is ... 135
Lucky number is ... 5566
Lucky number is ... 333
Lucky number is ... 45
Lucky number is ... 45
Lucky number is ... 68
Lucky number is ... 68
Lucky number is ... 333
Lucky number is ... 78
Lucky number is ... 78
Lucky number is ... 5566
Lucky number is ... 78
Lucky number is ... 333
Lucky number is ... 135


I want a result like this



Lucky number is ... 135
Lucky number is ... 5566
Lucky number is ... 78
Lucky number is ... 190
Lucky number is ... 333
Lucky number is ... 45
Lucky number is ... 68


So we can get the "Lucky number" without dups,unique
Someone know how to do?
Editing the script and reduced 21 to 9 don't change,I get even duplicates.



Lucky number is ... 135
Lucky number is ... 68
Lucky number is ... 45
Lucky number is ... 333
Lucky number is ... 78
Lucky number is ... 135
Lucky number is ... 135
Lucky number is ... 333
Lucky number is ... 32









share|improve this question




















  • 1





    you asked for 21 random numbers from a set of 9 numbers ... I'm not sure what you expected to happen there?

    – Jeff Schaller
    1 hour ago











  • I have reduced 21 to 9 and is the same thing. Give me a 9 numbers but with some repeat for one or more times

    – elbarna
    1 hour ago











  • do an unset array[index] after you’ve echoed your Lucky number ? or piping your output to the uniq command ?

    – LL3
    1 hour ago


















0















Suppose a script like this



#!/bin/bash
array[0]="123"
array[1]="333"
array[2]="5566"
array[3]="135"
array[4]="68"
array[5]="45"
array[6]="78"
array[7]="32"
array[8]="190"

number()
{
size=${#array[@]}
index=$(($RANDOM % $size))
sleep 0.5s
echo Lucky number is ... ${array[$index]}
}

for i in {1..21}; do number; done


Is ok..I want to get random number in the interval,but show also a lot of dups



Lucky number is ... 135
Lucky number is ... 135
Lucky number is ... 5566
Lucky number is ... 78
Lucky number is ... 190
Lucky number is ... 333
Lucky number is ... 190
Lucky number is ... 135
Lucky number is ... 5566
Lucky number is ... 333
Lucky number is ... 45
Lucky number is ... 45
Lucky number is ... 68
Lucky number is ... 68
Lucky number is ... 333
Lucky number is ... 78
Lucky number is ... 78
Lucky number is ... 5566
Lucky number is ... 78
Lucky number is ... 333
Lucky number is ... 135


I want a result like this



Lucky number is ... 135
Lucky number is ... 5566
Lucky number is ... 78
Lucky number is ... 190
Lucky number is ... 333
Lucky number is ... 45
Lucky number is ... 68


So we can get the "Lucky number" without dups,unique
Someone know how to do?
Editing the script and reduced 21 to 9 don't change,I get even duplicates.



Lucky number is ... 135
Lucky number is ... 68
Lucky number is ... 45
Lucky number is ... 333
Lucky number is ... 78
Lucky number is ... 135
Lucky number is ... 135
Lucky number is ... 333
Lucky number is ... 32









share|improve this question




















  • 1





    you asked for 21 random numbers from a set of 9 numbers ... I'm not sure what you expected to happen there?

    – Jeff Schaller
    1 hour ago











  • I have reduced 21 to 9 and is the same thing. Give me a 9 numbers but with some repeat for one or more times

    – elbarna
    1 hour ago











  • do an unset array[index] after you’ve echoed your Lucky number ? or piping your output to the uniq command ?

    – LL3
    1 hour ago














0












0








0








Suppose a script like this



#!/bin/bash
array[0]="123"
array[1]="333"
array[2]="5566"
array[3]="135"
array[4]="68"
array[5]="45"
array[6]="78"
array[7]="32"
array[8]="190"

number()
{
size=${#array[@]}
index=$(($RANDOM % $size))
sleep 0.5s
echo Lucky number is ... ${array[$index]}
}

for i in {1..21}; do number; done


Is ok..I want to get random number in the interval,but show also a lot of dups



Lucky number is ... 135
Lucky number is ... 135
Lucky number is ... 5566
Lucky number is ... 78
Lucky number is ... 190
Lucky number is ... 333
Lucky number is ... 190
Lucky number is ... 135
Lucky number is ... 5566
Lucky number is ... 333
Lucky number is ... 45
Lucky number is ... 45
Lucky number is ... 68
Lucky number is ... 68
Lucky number is ... 333
Lucky number is ... 78
Lucky number is ... 78
Lucky number is ... 5566
Lucky number is ... 78
Lucky number is ... 333
Lucky number is ... 135


I want a result like this



Lucky number is ... 135
Lucky number is ... 5566
Lucky number is ... 78
Lucky number is ... 190
Lucky number is ... 333
Lucky number is ... 45
Lucky number is ... 68


So we can get the "Lucky number" without dups,unique
Someone know how to do?
Editing the script and reduced 21 to 9 don't change,I get even duplicates.



Lucky number is ... 135
Lucky number is ... 68
Lucky number is ... 45
Lucky number is ... 333
Lucky number is ... 78
Lucky number is ... 135
Lucky number is ... 135
Lucky number is ... 333
Lucky number is ... 32









share|improve this question
















Suppose a script like this



#!/bin/bash
array[0]="123"
array[1]="333"
array[2]="5566"
array[3]="135"
array[4]="68"
array[5]="45"
array[6]="78"
array[7]="32"
array[8]="190"

number()
{
size=${#array[@]}
index=$(($RANDOM % $size))
sleep 0.5s
echo Lucky number is ... ${array[$index]}
}

for i in {1..21}; do number; done


Is ok..I want to get random number in the interval,but show also a lot of dups



Lucky number is ... 135
Lucky number is ... 135
Lucky number is ... 5566
Lucky number is ... 78
Lucky number is ... 190
Lucky number is ... 333
Lucky number is ... 190
Lucky number is ... 135
Lucky number is ... 5566
Lucky number is ... 333
Lucky number is ... 45
Lucky number is ... 45
Lucky number is ... 68
Lucky number is ... 68
Lucky number is ... 333
Lucky number is ... 78
Lucky number is ... 78
Lucky number is ... 5566
Lucky number is ... 78
Lucky number is ... 333
Lucky number is ... 135


I want a result like this



Lucky number is ... 135
Lucky number is ... 5566
Lucky number is ... 78
Lucky number is ... 190
Lucky number is ... 333
Lucky number is ... 45
Lucky number is ... 68


So we can get the "Lucky number" without dups,unique
Someone know how to do?
Editing the script and reduced 21 to 9 don't change,I get even duplicates.



Lucky number is ... 135
Lucky number is ... 68
Lucky number is ... 45
Lucky number is ... 333
Lucky number is ... 78
Lucky number is ... 135
Lucky number is ... 135
Lucky number is ... 333
Lucky number is ... 32






bash random






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago







elbarna

















asked 1 hour ago









elbarnaelbarna

4,42612 gold badges43 silver badges96 bronze badges




4,42612 gold badges43 silver badges96 bronze badges








  • 1





    you asked for 21 random numbers from a set of 9 numbers ... I'm not sure what you expected to happen there?

    – Jeff Schaller
    1 hour ago











  • I have reduced 21 to 9 and is the same thing. Give me a 9 numbers but with some repeat for one or more times

    – elbarna
    1 hour ago











  • do an unset array[index] after you’ve echoed your Lucky number ? or piping your output to the uniq command ?

    – LL3
    1 hour ago














  • 1





    you asked for 21 random numbers from a set of 9 numbers ... I'm not sure what you expected to happen there?

    – Jeff Schaller
    1 hour ago











  • I have reduced 21 to 9 and is the same thing. Give me a 9 numbers but with some repeat for one or more times

    – elbarna
    1 hour ago











  • do an unset array[index] after you’ve echoed your Lucky number ? or piping your output to the uniq command ?

    – LL3
    1 hour ago








1




1





you asked for 21 random numbers from a set of 9 numbers ... I'm not sure what you expected to happen there?

– Jeff Schaller
1 hour ago





you asked for 21 random numbers from a set of 9 numbers ... I'm not sure what you expected to happen there?

– Jeff Schaller
1 hour ago













I have reduced 21 to 9 and is the same thing. Give me a 9 numbers but with some repeat for one or more times

– elbarna
1 hour ago





I have reduced 21 to 9 and is the same thing. Give me a 9 numbers but with some repeat for one or more times

– elbarna
1 hour ago













do an unset array[index] after you’ve echoed your Lucky number ? or piping your output to the uniq command ?

– LL3
1 hour ago





do an unset array[index] after you’ve echoed your Lucky number ? or piping your output to the uniq command ?

– LL3
1 hour ago










1 Answer
1






active

oldest

votes


















2














Treat the list like a deck of cards. Shuffle the numbers, then read them out one at a time.



See Simple method to shuffle the elements of an array in BASH shell?.



#!/bin/bash
array=("123" "333" "5566" "135" "68" "45" "78" "32" "190")

shuffle() {
array=($(shuf -e "${array[@]}"))
index=0
}

number() {
size=${#array[@]}
sleep 0.5s

if (( index >= size )) ; then
shuffle
fi

echo Lucky number is ... ${array[$index]}
index=$((index + 1))
}

for i in {1..21}; do number; done





share|improve this answer


























  • @steeldriver Advantage of shuffling the indices is the original array is left alone. But you'd still have to create an array of indices and track position within it to prevent duplicates. Otherwise, the shuf command may return the same index multiple times.

    – xiota
    1 hour ago














Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f526723%2fbash-how-to-avoid-duplicate-result-from-random-list%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Treat the list like a deck of cards. Shuffle the numbers, then read them out one at a time.



See Simple method to shuffle the elements of an array in BASH shell?.



#!/bin/bash
array=("123" "333" "5566" "135" "68" "45" "78" "32" "190")

shuffle() {
array=($(shuf -e "${array[@]}"))
index=0
}

number() {
size=${#array[@]}
sleep 0.5s

if (( index >= size )) ; then
shuffle
fi

echo Lucky number is ... ${array[$index]}
index=$((index + 1))
}

for i in {1..21}; do number; done





share|improve this answer


























  • @steeldriver Advantage of shuffling the indices is the original array is left alone. But you'd still have to create an array of indices and track position within it to prevent duplicates. Otherwise, the shuf command may return the same index multiple times.

    – xiota
    1 hour ago
















2














Treat the list like a deck of cards. Shuffle the numbers, then read them out one at a time.



See Simple method to shuffle the elements of an array in BASH shell?.



#!/bin/bash
array=("123" "333" "5566" "135" "68" "45" "78" "32" "190")

shuffle() {
array=($(shuf -e "${array[@]}"))
index=0
}

number() {
size=${#array[@]}
sleep 0.5s

if (( index >= size )) ; then
shuffle
fi

echo Lucky number is ... ${array[$index]}
index=$((index + 1))
}

for i in {1..21}; do number; done





share|improve this answer


























  • @steeldriver Advantage of shuffling the indices is the original array is left alone. But you'd still have to create an array of indices and track position within it to prevent duplicates. Otherwise, the shuf command may return the same index multiple times.

    – xiota
    1 hour ago














2












2








2







Treat the list like a deck of cards. Shuffle the numbers, then read them out one at a time.



See Simple method to shuffle the elements of an array in BASH shell?.



#!/bin/bash
array=("123" "333" "5566" "135" "68" "45" "78" "32" "190")

shuffle() {
array=($(shuf -e "${array[@]}"))
index=0
}

number() {
size=${#array[@]}
sleep 0.5s

if (( index >= size )) ; then
shuffle
fi

echo Lucky number is ... ${array[$index]}
index=$((index + 1))
}

for i in {1..21}; do number; done





share|improve this answer















Treat the list like a deck of cards. Shuffle the numbers, then read them out one at a time.



See Simple method to shuffle the elements of an array in BASH shell?.



#!/bin/bash
array=("123" "333" "5566" "135" "68" "45" "78" "32" "190")

shuffle() {
array=($(shuf -e "${array[@]}"))
index=0
}

number() {
size=${#array[@]}
sleep 0.5s

if (( index >= size )) ; then
shuffle
fi

echo Lucky number is ... ${array[$index]}
index=$((index + 1))
}

for i in {1..21}; do number; done






share|improve this answer














share|improve this answer



share|improve this answer








edited 1 hour ago

























answered 1 hour ago









xiotaxiota

1258 bronze badges




1258 bronze badges













  • @steeldriver Advantage of shuffling the indices is the original array is left alone. But you'd still have to create an array of indices and track position within it to prevent duplicates. Otherwise, the shuf command may return the same index multiple times.

    – xiota
    1 hour ago



















  • @steeldriver Advantage of shuffling the indices is the original array is left alone. But you'd still have to create an array of indices and track position within it to prevent duplicates. Otherwise, the shuf command may return the same index multiple times.

    – xiota
    1 hour ago

















@steeldriver Advantage of shuffling the indices is the original array is left alone. But you'd still have to create an array of indices and track position within it to prevent duplicates. Otherwise, the shuf command may return the same index multiple times.

– xiota
1 hour ago





@steeldriver Advantage of shuffling the indices is the original array is left alone. But you'd still have to create an array of indices and track position within it to prevent duplicates. Otherwise, the shuf command may return the same index multiple times.

– xiota
1 hour ago


















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f526723%2fbash-how-to-avoid-duplicate-result-from-random-list%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Hudson River Historic District Contents Geography History The district today Aesthetics Cultural...

The number designs the writing. Feandra Aversely Definition: The act of ingrafting a sprig or shoot of one...

Ayherre Geografie Demografie Externe links Navigatiemenu43° 23′ NB, 1° 15′ WL43° 23′ NB, 1°...