Assigning random values to the elements of a list





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















From this question we can construct an interleaved list out of two lists with a different length (according to Sylwester answer making use of circular-list). My question is how can we do the same but taking the elements of the second list randomly.



That is, I want to assign to the elements of a list a random value out of a list. For example: given the assigning list '(0 1) and whatever list '(a b c d) I want as the output a list such as '((a 0) (b 0) (c 1) (d 0)) or '((a 1) (b 0) (c 0) (d 1)) for example.



My attempts are:





  1. (map cons '(a b c d) (circular-list (random '(0 1))))




which gives '((a . 0) (b . 0) (c . 0) (d . 0)) or '((a . 1) (b . 1) (c . 1) (d . 1)) and nothing different and





  1. (map cons '(a b c d) (list (random '(0 1))))




which gives '((a . 0)) or '((a . 1)) and nothing different.



PS: I am making use of a function called random and defined as




(define (random lst) (list-ref lst (random (length lst))))




which in my example would take randomly 0 or 1 out of a list '(0 1).










share|improve this question

























  • What is the result of (random '(0) '(1)) ? (Which language are you using? (Racket, one of the teaching languages, other)

    – soegaard
    Jan 3 at 10:33











  • See answer below.

    – soegaard
    Jan 3 at 10:38


















1















From this question we can construct an interleaved list out of two lists with a different length (according to Sylwester answer making use of circular-list). My question is how can we do the same but taking the elements of the second list randomly.



That is, I want to assign to the elements of a list a random value out of a list. For example: given the assigning list '(0 1) and whatever list '(a b c d) I want as the output a list such as '((a 0) (b 0) (c 1) (d 0)) or '((a 1) (b 0) (c 0) (d 1)) for example.



My attempts are:





  1. (map cons '(a b c d) (circular-list (random '(0 1))))




which gives '((a . 0) (b . 0) (c . 0) (d . 0)) or '((a . 1) (b . 1) (c . 1) (d . 1)) and nothing different and





  1. (map cons '(a b c d) (list (random '(0 1))))




which gives '((a . 0)) or '((a . 1)) and nothing different.



PS: I am making use of a function called random and defined as




(define (random lst) (list-ref lst (random (length lst))))




which in my example would take randomly 0 or 1 out of a list '(0 1).










share|improve this question

























  • What is the result of (random '(0) '(1)) ? (Which language are you using? (Racket, one of the teaching languages, other)

    – soegaard
    Jan 3 at 10:33











  • See answer below.

    – soegaard
    Jan 3 at 10:38














1












1








1


1






From this question we can construct an interleaved list out of two lists with a different length (according to Sylwester answer making use of circular-list). My question is how can we do the same but taking the elements of the second list randomly.



That is, I want to assign to the elements of a list a random value out of a list. For example: given the assigning list '(0 1) and whatever list '(a b c d) I want as the output a list such as '((a 0) (b 0) (c 1) (d 0)) or '((a 1) (b 0) (c 0) (d 1)) for example.



My attempts are:





  1. (map cons '(a b c d) (circular-list (random '(0 1))))




which gives '((a . 0) (b . 0) (c . 0) (d . 0)) or '((a . 1) (b . 1) (c . 1) (d . 1)) and nothing different and





  1. (map cons '(a b c d) (list (random '(0 1))))




which gives '((a . 0)) or '((a . 1)) and nothing different.



PS: I am making use of a function called random and defined as




(define (random lst) (list-ref lst (random (length lst))))




which in my example would take randomly 0 or 1 out of a list '(0 1).










share|improve this question
















From this question we can construct an interleaved list out of two lists with a different length (according to Sylwester answer making use of circular-list). My question is how can we do the same but taking the elements of the second list randomly.



That is, I want to assign to the elements of a list a random value out of a list. For example: given the assigning list '(0 1) and whatever list '(a b c d) I want as the output a list such as '((a 0) (b 0) (c 1) (d 0)) or '((a 1) (b 0) (c 0) (d 1)) for example.



My attempts are:





  1. (map cons '(a b c d) (circular-list (random '(0 1))))




which gives '((a . 0) (b . 0) (c . 0) (d . 0)) or '((a . 1) (b . 1) (c . 1) (d . 1)) and nothing different and





  1. (map cons '(a b c d) (list (random '(0 1))))




which gives '((a . 0)) or '((a . 1)) and nothing different.



PS: I am making use of a function called random and defined as




(define (random lst) (list-ref lst (random (length lst))))




which in my example would take randomly 0 or 1 out of a list '(0 1).







list random racket






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 13:46







gibarian

















asked Jan 3 at 9:56









gibariangibarian

646




646













  • What is the result of (random '(0) '(1)) ? (Which language are you using? (Racket, one of the teaching languages, other)

    – soegaard
    Jan 3 at 10:33











  • See answer below.

    – soegaard
    Jan 3 at 10:38



















  • What is the result of (random '(0) '(1)) ? (Which language are you using? (Racket, one of the teaching languages, other)

    – soegaard
    Jan 3 at 10:33











  • See answer below.

    – soegaard
    Jan 3 at 10:38

















What is the result of (random '(0) '(1)) ? (Which language are you using? (Racket, one of the teaching languages, other)

– soegaard
Jan 3 at 10:33





What is the result of (random '(0) '(1)) ? (Which language are you using? (Racket, one of the teaching languages, other)

– soegaard
Jan 3 at 10:33













See answer below.

– soegaard
Jan 3 at 10:38





See answer below.

– soegaard
Jan 3 at 10:38












2 Answers
2






active

oldest

votes


















2














To avoid confusion, you should rename your new "random" function on lists to random-element or something similar. That way both people reading your code, and Racket, will know the difference.



random : PositiveNaturalNumber -> NaturalNumber

random-element : [NonEmptyListof X] -> X


These two different functions need to have two different names so that when you want to refer to the first one random from Racket, you can do so. Otherwise Racket (as well as other people reading your code) will think you mean random-element when you really want random.



This confusion matters in the body of your definition of "random":



;           /--------------<< This should be renamed to `random-element`
; /
(define (random lst)
(list-ref lst (random (length lst))))
; /
; ------<< This is meant to be the original Racket `random`
; but it ends up referring to "random-element" because
; of the naming conflict


Because of this name conflict, when this definition of random is put in a File, and I run a Repl for that File, I get an error like this:



> (random '(A B C D E F G))
length: contract violation
expected: list?
given: 7


When this is renamed, the definition should look like this:



;; random-element : [NonEmptyListof X] -> X
(define (random-element lst)
(list-ref lst (random (length lst))))
; /
; -------<< This is the `random` from Racket, not the "new" one


Using it:



> (random-element '(A B C D E F G))
'E
> (random-element '(A B C D E F G))
'B
> (random-element '(A B C D E F G))
'D
> (random-element '(A B C D E F G))
'F
> (random-element '(A B C D E F G))
'C





share|improve this answer
























  • Thank you very much for the clarifcation. It has been formative. However, my function still gives the same value for all (a b c d): '((a . 0) (b . 0) (c . 0) (d . 0)) or '((a . 1) (b . 1) (c . 1) (d . 1)). @PetSerAl answer works fine but I don't understand why we need to convert the list into a vector yet (let alone the fact that it does not work in DrRacket, only in the Ubuntu terminal).

    – gibarian
    Jan 3 at 18:44













  • You shouldn't need to convert it to a vector. However, I suspect that my answer here combined with an answer to How to create a list with n applications of a procedure will solve that problem.

    – Alex Knauth
    Jan 3 at 19:47



















0














The comments can't contain code fragments, so ...



What does this program give?



#lang racket
(require srfi/1)
(random '(0) '(1))


On my computer with Racket 7.0 I get:



random: contract violation
expected: exact-integer?
given: '(0)
argument position: 1st
other arguments...:





share|improve this answer
























  • 'random' is a function I have written. It is actually more complex, I wanted to simplify the question since my interest is on the "interleaving part". For example: (define (random lista) (list-ref lista (random (length lista)))). Sorry for that.

    – gibarian
    Jan 3 at 11:15











  • In my Ubuntu terminal (random '(0 1)) returns 0 or 1.

    – gibarian
    Jan 3 at 11:21














Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f54019907%2fassigning-random-values-to-the-elements-of-a-list%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














To avoid confusion, you should rename your new "random" function on lists to random-element or something similar. That way both people reading your code, and Racket, will know the difference.



random : PositiveNaturalNumber -> NaturalNumber

random-element : [NonEmptyListof X] -> X


These two different functions need to have two different names so that when you want to refer to the first one random from Racket, you can do so. Otherwise Racket (as well as other people reading your code) will think you mean random-element when you really want random.



This confusion matters in the body of your definition of "random":



;           /--------------<< This should be renamed to `random-element`
; /
(define (random lst)
(list-ref lst (random (length lst))))
; /
; ------<< This is meant to be the original Racket `random`
; but it ends up referring to "random-element" because
; of the naming conflict


Because of this name conflict, when this definition of random is put in a File, and I run a Repl for that File, I get an error like this:



> (random '(A B C D E F G))
length: contract violation
expected: list?
given: 7


When this is renamed, the definition should look like this:



;; random-element : [NonEmptyListof X] -> X
(define (random-element lst)
(list-ref lst (random (length lst))))
; /
; -------<< This is the `random` from Racket, not the "new" one


Using it:



> (random-element '(A B C D E F G))
'E
> (random-element '(A B C D E F G))
'B
> (random-element '(A B C D E F G))
'D
> (random-element '(A B C D E F G))
'F
> (random-element '(A B C D E F G))
'C





share|improve this answer
























  • Thank you very much for the clarifcation. It has been formative. However, my function still gives the same value for all (a b c d): '((a . 0) (b . 0) (c . 0) (d . 0)) or '((a . 1) (b . 1) (c . 1) (d . 1)). @PetSerAl answer works fine but I don't understand why we need to convert the list into a vector yet (let alone the fact that it does not work in DrRacket, only in the Ubuntu terminal).

    – gibarian
    Jan 3 at 18:44













  • You shouldn't need to convert it to a vector. However, I suspect that my answer here combined with an answer to How to create a list with n applications of a procedure will solve that problem.

    – Alex Knauth
    Jan 3 at 19:47
















2














To avoid confusion, you should rename your new "random" function on lists to random-element or something similar. That way both people reading your code, and Racket, will know the difference.



random : PositiveNaturalNumber -> NaturalNumber

random-element : [NonEmptyListof X] -> X


These two different functions need to have two different names so that when you want to refer to the first one random from Racket, you can do so. Otherwise Racket (as well as other people reading your code) will think you mean random-element when you really want random.



This confusion matters in the body of your definition of "random":



;           /--------------<< This should be renamed to `random-element`
; /
(define (random lst)
(list-ref lst (random (length lst))))
; /
; ------<< This is meant to be the original Racket `random`
; but it ends up referring to "random-element" because
; of the naming conflict


Because of this name conflict, when this definition of random is put in a File, and I run a Repl for that File, I get an error like this:



> (random '(A B C D E F G))
length: contract violation
expected: list?
given: 7


When this is renamed, the definition should look like this:



;; random-element : [NonEmptyListof X] -> X
(define (random-element lst)
(list-ref lst (random (length lst))))
; /
; -------<< This is the `random` from Racket, not the "new" one


Using it:



> (random-element '(A B C D E F G))
'E
> (random-element '(A B C D E F G))
'B
> (random-element '(A B C D E F G))
'D
> (random-element '(A B C D E F G))
'F
> (random-element '(A B C D E F G))
'C





share|improve this answer
























  • Thank you very much for the clarifcation. It has been formative. However, my function still gives the same value for all (a b c d): '((a . 0) (b . 0) (c . 0) (d . 0)) or '((a . 1) (b . 1) (c . 1) (d . 1)). @PetSerAl answer works fine but I don't understand why we need to convert the list into a vector yet (let alone the fact that it does not work in DrRacket, only in the Ubuntu terminal).

    – gibarian
    Jan 3 at 18:44













  • You shouldn't need to convert it to a vector. However, I suspect that my answer here combined with an answer to How to create a list with n applications of a procedure will solve that problem.

    – Alex Knauth
    Jan 3 at 19:47














2












2








2







To avoid confusion, you should rename your new "random" function on lists to random-element or something similar. That way both people reading your code, and Racket, will know the difference.



random : PositiveNaturalNumber -> NaturalNumber

random-element : [NonEmptyListof X] -> X


These two different functions need to have two different names so that when you want to refer to the first one random from Racket, you can do so. Otherwise Racket (as well as other people reading your code) will think you mean random-element when you really want random.



This confusion matters in the body of your definition of "random":



;           /--------------<< This should be renamed to `random-element`
; /
(define (random lst)
(list-ref lst (random (length lst))))
; /
; ------<< This is meant to be the original Racket `random`
; but it ends up referring to "random-element" because
; of the naming conflict


Because of this name conflict, when this definition of random is put in a File, and I run a Repl for that File, I get an error like this:



> (random '(A B C D E F G))
length: contract violation
expected: list?
given: 7


When this is renamed, the definition should look like this:



;; random-element : [NonEmptyListof X] -> X
(define (random-element lst)
(list-ref lst (random (length lst))))
; /
; -------<< This is the `random` from Racket, not the "new" one


Using it:



> (random-element '(A B C D E F G))
'E
> (random-element '(A B C D E F G))
'B
> (random-element '(A B C D E F G))
'D
> (random-element '(A B C D E F G))
'F
> (random-element '(A B C D E F G))
'C





share|improve this answer













To avoid confusion, you should rename your new "random" function on lists to random-element or something similar. That way both people reading your code, and Racket, will know the difference.



random : PositiveNaturalNumber -> NaturalNumber

random-element : [NonEmptyListof X] -> X


These two different functions need to have two different names so that when you want to refer to the first one random from Racket, you can do so. Otherwise Racket (as well as other people reading your code) will think you mean random-element when you really want random.



This confusion matters in the body of your definition of "random":



;           /--------------<< This should be renamed to `random-element`
; /
(define (random lst)
(list-ref lst (random (length lst))))
; /
; ------<< This is meant to be the original Racket `random`
; but it ends up referring to "random-element" because
; of the naming conflict


Because of this name conflict, when this definition of random is put in a File, and I run a Repl for that File, I get an error like this:



> (random '(A B C D E F G))
length: contract violation
expected: list?
given: 7


When this is renamed, the definition should look like this:



;; random-element : [NonEmptyListof X] -> X
(define (random-element lst)
(list-ref lst (random (length lst))))
; /
; -------<< This is the `random` from Racket, not the "new" one


Using it:



> (random-element '(A B C D E F G))
'E
> (random-element '(A B C D E F G))
'B
> (random-element '(A B C D E F G))
'D
> (random-element '(A B C D E F G))
'F
> (random-element '(A B C D E F G))
'C






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 3 at 17:29









Alex KnauthAlex Knauth

5,2221823




5,2221823













  • Thank you very much for the clarifcation. It has been formative. However, my function still gives the same value for all (a b c d): '((a . 0) (b . 0) (c . 0) (d . 0)) or '((a . 1) (b . 1) (c . 1) (d . 1)). @PetSerAl answer works fine but I don't understand why we need to convert the list into a vector yet (let alone the fact that it does not work in DrRacket, only in the Ubuntu terminal).

    – gibarian
    Jan 3 at 18:44













  • You shouldn't need to convert it to a vector. However, I suspect that my answer here combined with an answer to How to create a list with n applications of a procedure will solve that problem.

    – Alex Knauth
    Jan 3 at 19:47



















  • Thank you very much for the clarifcation. It has been formative. However, my function still gives the same value for all (a b c d): '((a . 0) (b . 0) (c . 0) (d . 0)) or '((a . 1) (b . 1) (c . 1) (d . 1)). @PetSerAl answer works fine but I don't understand why we need to convert the list into a vector yet (let alone the fact that it does not work in DrRacket, only in the Ubuntu terminal).

    – gibarian
    Jan 3 at 18:44













  • You shouldn't need to convert it to a vector. However, I suspect that my answer here combined with an answer to How to create a list with n applications of a procedure will solve that problem.

    – Alex Knauth
    Jan 3 at 19:47

















Thank you very much for the clarifcation. It has been formative. However, my function still gives the same value for all (a b c d): '((a . 0) (b . 0) (c . 0) (d . 0)) or '((a . 1) (b . 1) (c . 1) (d . 1)). @PetSerAl answer works fine but I don't understand why we need to convert the list into a vector yet (let alone the fact that it does not work in DrRacket, only in the Ubuntu terminal).

– gibarian
Jan 3 at 18:44







Thank you very much for the clarifcation. It has been formative. However, my function still gives the same value for all (a b c d): '((a . 0) (b . 0) (c . 0) (d . 0)) or '((a . 1) (b . 1) (c . 1) (d . 1)). @PetSerAl answer works fine but I don't understand why we need to convert the list into a vector yet (let alone the fact that it does not work in DrRacket, only in the Ubuntu terminal).

– gibarian
Jan 3 at 18:44















You shouldn't need to convert it to a vector. However, I suspect that my answer here combined with an answer to How to create a list with n applications of a procedure will solve that problem.

– Alex Knauth
Jan 3 at 19:47





You shouldn't need to convert it to a vector. However, I suspect that my answer here combined with an answer to How to create a list with n applications of a procedure will solve that problem.

– Alex Knauth
Jan 3 at 19:47













0














The comments can't contain code fragments, so ...



What does this program give?



#lang racket
(require srfi/1)
(random '(0) '(1))


On my computer with Racket 7.0 I get:



random: contract violation
expected: exact-integer?
given: '(0)
argument position: 1st
other arguments...:





share|improve this answer
























  • 'random' is a function I have written. It is actually more complex, I wanted to simplify the question since my interest is on the "interleaving part". For example: (define (random lista) (list-ref lista (random (length lista)))). Sorry for that.

    – gibarian
    Jan 3 at 11:15











  • In my Ubuntu terminal (random '(0 1)) returns 0 or 1.

    – gibarian
    Jan 3 at 11:21


















0














The comments can't contain code fragments, so ...



What does this program give?



#lang racket
(require srfi/1)
(random '(0) '(1))


On my computer with Racket 7.0 I get:



random: contract violation
expected: exact-integer?
given: '(0)
argument position: 1st
other arguments...:





share|improve this answer
























  • 'random' is a function I have written. It is actually more complex, I wanted to simplify the question since my interest is on the "interleaving part". For example: (define (random lista) (list-ref lista (random (length lista)))). Sorry for that.

    – gibarian
    Jan 3 at 11:15











  • In my Ubuntu terminal (random '(0 1)) returns 0 or 1.

    – gibarian
    Jan 3 at 11:21
















0












0








0







The comments can't contain code fragments, so ...



What does this program give?



#lang racket
(require srfi/1)
(random '(0) '(1))


On my computer with Racket 7.0 I get:



random: contract violation
expected: exact-integer?
given: '(0)
argument position: 1st
other arguments...:





share|improve this answer













The comments can't contain code fragments, so ...



What does this program give?



#lang racket
(require srfi/1)
(random '(0) '(1))


On my computer with Racket 7.0 I get:



random: contract violation
expected: exact-integer?
given: '(0)
argument position: 1st
other arguments...:






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 3 at 10:38









soegaardsoegaard

24.6k44080




24.6k44080













  • 'random' is a function I have written. It is actually more complex, I wanted to simplify the question since my interest is on the "interleaving part". For example: (define (random lista) (list-ref lista (random (length lista)))). Sorry for that.

    – gibarian
    Jan 3 at 11:15











  • In my Ubuntu terminal (random '(0 1)) returns 0 or 1.

    – gibarian
    Jan 3 at 11:21





















  • 'random' is a function I have written. It is actually more complex, I wanted to simplify the question since my interest is on the "interleaving part". For example: (define (random lista) (list-ref lista (random (length lista)))). Sorry for that.

    – gibarian
    Jan 3 at 11:15











  • In my Ubuntu terminal (random '(0 1)) returns 0 or 1.

    – gibarian
    Jan 3 at 11:21



















'random' is a function I have written. It is actually more complex, I wanted to simplify the question since my interest is on the "interleaving part". For example: (define (random lista) (list-ref lista (random (length lista)))). Sorry for that.

– gibarian
Jan 3 at 11:15





'random' is a function I have written. It is actually more complex, I wanted to simplify the question since my interest is on the "interleaving part". For example: (define (random lista) (list-ref lista (random (length lista)))). Sorry for that.

– gibarian
Jan 3 at 11:15













In my Ubuntu terminal (random '(0 1)) returns 0 or 1.

– gibarian
Jan 3 at 11:21







In my Ubuntu terminal (random '(0 1)) returns 0 or 1.

– gibarian
Jan 3 at 11:21




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • 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%2fstackoverflow.com%2fquestions%2f54019907%2fassigning-random-values-to-the-elements-of-a-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

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

How to fix TextFormField cause rebuild widget in Flutter