How to use the age function from chance

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

chance.age() is a function in the Chance library that generates a random age.

75
76
77
78
79
80
81
82
83
  if (opts.natural) return chance.natural(mm);
  if (opts.float) return chance.floating(helpers.mix(mm, fix));
  if (opts.dice) return chance.integer({min: 1, max: Number(opts.dice)});
  if (opts.age) {
    if (opts.age === true) return chance.age();
    return chance.age({type: opts.age});
  }
  return chance.integer(mm);
};
fork icon12
star icon33
watch icon3

+ 47 other calls in file

33
34
35
36
37
38
39
40
41
42
}
for (j = 1; j <= rows; j++) { // for each row
    for (k = 0; k < cols; k++) { // for each field, generate data cell
        switch (hdr[k].keyword) {
            case 'age':
                s += chance.age();
                break;
            case 'alpha': //alpha or alpha(n)
                n = hdr[k].arg;
                if (n != "") {
fork icon2
star icon8
watch icon0

How does chance.age work?

chance.age() is a method in the Chance.js library that generates a random age between 0 and 115 by generating a random number between 0 and 1 and then mapping it to a range of possible ages. The age is returned as an integer.

17
18
19
20
21
22
23
24
25
26
const person = {
    type: spec.label,
    prefix: chance.prefix({gender: gender }),
    first: chance.first({gender: gender}),
    last: chance.last(),
    age: chance.age(),
    birthday: chance.birthday({string: true, american: true}),
    gender: gender,
    zip: chance.zip({plusfour: true}),
    ssnFour: chance.ssn({ ssnFour: true }),
fork icon0
star icon1
watch icon0

8
9
10
11
12
13
14
15
16
17
const data = {
    headers: ["Name","Age","Prof","Country"],
    rows: new Array(30).fill(undefined).map(() => {
        return [
            chance.name(),
            chance.age(),
            chance.profession(),
            chance.country({full: true})
        ]
    })
fork icon0
star icon0
watch icon1

+ 2 other calls in file

Ai Example

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

const age = chance.age({ type: "adult" }); // generates a random age between 18 and 65
console.log(age); // outputs a random number between 18 and 65