How to use the pickone function from chance

Find comprehensive JavaScript chance.pickone code examples handpicked from public code repositorys.

chance.pickone is a function that returns a randomly chosen element from an array provided as input.

97
98
99
100
101
102
103
104
105
106

setTimeout(() => {
    active = false;
    if (!data.args[0].includes(`stop`)) {
        if (entrees.length > 0) {
            const winner = chance.pickone(entrees);
            client.sendMessage(Bot.translate("plugins.giveaway.winner", {
                user: winner
            }));
        } else {
fork icon54
star icon36
watch icon0

138
139
140
141
142
143
144
145
146
147
/**
 * Generate a random first name.
 * @param {boolean} cn
 */
firstName(cn = false) {
  return cn ? chance.pickone(chinese.firstNames) : chance.firstName();
},

/**
 * Generate a random last name.
fork icon5
star icon0
watch icon1

+ 27 other calls in file

How does chance.pickone work?

chance.pickone is a function in the Chance.js library that takes an array of elements and returns a randomly selected element from that array. It uses the built-in Math.random() method to generate a random index within the range of the input array and returns the element at that index.

31
32
33
34
35
36
37
38
39
40
lastName: chance.last(),
isAdmin: false,
email: emails.pop(),
gender: chance.gender(),
dob: chance.birthday({ string: true }),
occupation: chance.pickone(['Sales', 'Hospitality', 'Healthcare', 'Custodial', 'Accounting', 'Teaching', 'Law-Enforcement', 'Law', 'Finance', 'Engineering', 'Administration', 'Student', 'Other']),
incomeLevel: chance.weighted(['Under-$15,000', '$15,000-to-$24,999', '$25,000-to-$34,999', '$35,000-to-$49,999', '$50,000-to-$74,999', '$75,000-to-$99,999', '$100,000-to-$149,999', '$150,000-to-$199,999', '$200,000-and-over'], [11.6, 10.5, 10, 12.7, 16.7, 12.1, 14.1, 6.2, 6.1]),
ethnicity: chance.weighted(['White', 'Black', 'Hispanic', 'Asian', 'American-Indian/Alaska-Native', 'Hawaiian/Other-Pacific-Islander', 'Other'], [61, 12, 18, 6, 1, 0, 2]),
religion: chance.weighted(['Protestant', 'Catholic', 'Mormon', 'Judaism', 'Islam', 'Buddhism', 'Hinduism', 'Other', 'Unaffiliated'], [46.5, 20.8, 1.6, 1.9, 0.9, 0.7, 0.7, 3.5, 23.4]),
education: chance.weighted(['High-School', 'Some-College', 'Associate-Degree', 'Bachelor-Degree', 'Advanced-Degree'], [70, 15, 10, 4, 1]),
fork icon3
star icon12
watch icon6

+ 7 other calls in file

17
18
19
20
21
22
23
24
25
26

const jobs = await Job
  .create(jobsArray);

const notesArray = [...Array(noteCount)].map(() => {
  const job = chance.pickone(jobs);
  return {
    title: chance.country(),
    job: job._id,
    body: chance.sentence(),
fork icon2
star icon0
watch icon0

Ai Example

1
2
3
4
5
6
7
const Chance = require("chance");
const chance = new Chance();

const options = ["red", "green", "blue"];
const picked = chance.pickone(options);

console.log(picked); // outputs: 'blue' (for example)

In this example, an array options is defined with three possible colors. chance.pickone is then called with options as the argument, and a randomly selected color from the array is returned and assigned to picked. Finally, picked is logged to the console.

67
68
69
70
71
72
73
74
75
76
    expect(actualDuration).toBe(expectedDuration);
});

it('should only count the time a ticket has been in a production department/departmentStatus', () => {
    const nonProductionDepartment = getNonProductionDepartments()[0];
    const productionDepartment = chance.pickone(Object.keys(productionDepartmentsAndDepartmentStatuses));
    const durationSpentWithNonProductionDepartmentStatus = chance.floating({min: 0});
    const durationSpentWithProductionDepartmentStatus = chance.floating({min: 0});
    const workflowStepLedger = {
        [nonProductionDepartment]: {
fork icon0
star icon0
watch icon1

15
16
17
18
19
20
21
22
23
24
ticketAttributes = {
    TicketNumber:  String(chance.integer({min: 0})),
    Ship_by_Date: chance.date({string: true}),
    OrderDate: chance.date({string: true}),
    CustPONum: chance.string(),
    Priority: chance.pickone(getAllPriorities()),
    BillZip: chance.string(),
    BillCity: chance.string(),
    BillAddr1: chance.string(),
    BillAddr2: chance.string(),
fork icon0
star icon0
watch icon0

+ 71 other calls in file

265
266
267
268
269
270
271
272
273
274
    totalFramesRan: chance.d12(),
    jobComment: chance.string()
};
ticket = {
    destination: {
        department: chance.pickone(departmentsToPickFrom)
    },
    frameSize: chance.floating({min: 1, fixed: 2}),
    departmentToJobComment: {}
};
fork icon0
star icon0
watch icon0

+ 2 other calls in file

1
2
3
4
5
6
7
8
9
10
11
12
const chance = require('chance').Chance();


describe('helperService.js', () => {
    describe('getEmptyObjectIfUndefined()', () => {
        it('should return an empty object if an undefined value is passed in', () => {
            const undefinedValue = chance.pickone([undefined, null]);
            const expectedValue = {};


            const actualValue = helperService.getEmptyObjectIfUndefined(undefinedValue);

fork icon0
star icon0
watch icon0

33
34
35
36
37
38
39
40
41
42
    }, {})

}

function makeEvent() {
    const name = c.pickone(eventNames);
    return {
        name: name,
        segmentation: getSegmentation(name),
        meta: {
fork icon0
star icon0
watch icon0

+ 9 other calls in file

10
11
12
13
14
15
16
17
18
19
  if (is.plainObject(collection)) {
    return collection[chance.pickone(Object.keys(collection))]
  }

  if (is.string(collection)) {
    return chance.pickone(collection.split(separator))
  }

  return undefined
}
fork icon0
star icon0
watch icon1

+ 3 other calls in file

128
129
130
131
132
133
134
135
136
137
}
lameResponse = async (word) => {
  const quote = await getQuote()
  const start = ['No thine is ', 'Thee call me ', 'How dare thee say ', 'Ha! ']
  const exception = ['. Nonetheless, ', '. However, ', ', but ', '. Regardless ', ', yet ', ', even so '];
  return chance.pickone(start) + word + chance.pickone(exception) + quote;
}
getGoodbye = (text) => {
  if (!text) return
  const byeMatchRegex = text.match(/(goodbye|see ya|later|bye|adios|farewell|toodles|adieu|so long)/i)
fork icon0
star icon0
watch icon1

+ 47 other calls in file