Generate Random Numbers in a Knack App

Updated: Sep 24


Need to generate a random number in your Knack database app? There are a lot of use cases for this. Here I show you how to do that, along with dealing with some related items. Check it out, it is pretty simple.


Get a free Knack trial account here: https://www.knack.com/r/you-tube


Knack Article: https://learn.knack.com/article/gknuie4fg3-how-to-set-a-random-id


You can check out my Knack database services here: Knack!



Full Transcript

builders doing another video it's

pouring out rain here hope that audio

isn't

doesn't affect the audio but today's

topic i'll jump right into it

how to generate random numbers there

could be a number of use cases for this

but what i used it on

uh i hadn't built a

large complex app but it was for

students applying for certain classes in

the school system down in florida

and when they were applying they got

ranked in a certain way

uh i won't get into the but they came

out with a score

and there was only so many seats in each

class

and the top people

say there was 20 seats the top 20 scores

would get in but they wanted to deal

with

if

the

scores were the same for let's say the

number 19 20 21 22 or something well

they had to choose one of them because

they all had the same score and made the

cut and the state actually had some

mandate well you have to assign a random

number

and then whoever has the highest random

number will get in that's what happened

here i'll take you through how to do

that thanks

[Music]

okay let's do this

how to deal with the random numbers

by the way there's an article in that

has i think this is where i originally

learned it

uh

basically goes over what i go over

i'll put this in the youtube link

uh let's call up an app

i just have one of their

template apps i added this stuff to

so here's the deal if you want a random

number

you're going to create an equation field

so you know how to do that equation here

it is

called random id and you have to just

pick a function

and in this case

it's random

the article just uses random but i use

random int which gives me only integers

and then these two numbers here is what

it's going to fall between between 1

and

10 000. so those are the random stuff

that you're going to pick

um i believe it's possible that

if you had a lot of records a random

number potentially

may not be unique within your

table i'm not sure how you get around

that if someone has let me know so this

creates the random number

now i'm going to go to records

and you'll see this random number right

here

um

now this is an important note on how

this is how this behaves uh my scenario

or use case where we want to assign

a

random number

to each of the applications so they can

use that to

split hairs if you will if a lot of

people had the same score

for the cutoff point for a class

well the way this random things works

every time you update a record

it regenerates another random number and

in our case we didn't want that because

we wanted that same number to be

attached and not have these kids be

moving around and we have to

that place gets audited by

the state and they'll look at that and

if the number changed and this kid got

in but he had a lower score than someone

else so we had to deal with that but i'm

going to show you this

3045 that's the random number that was

generated now if you update you edit

anything in this record i'm going to

make this 2

into a 1. remember that was 3045.

it just regenerated to 866.

that behavior we don't want let me show

you how to get around that it's pretty

simple

we are going to add

a number field

and i'm going to just call this how

about static

random meaning

once it's set

it stays the same it stays the same

uh i don't believe if you click must be

unique

it will i think it will override that

and maybe i should test it i think in

this type of thing it will override it

when i talked about that unique thing

maybe not

okay

then we're going to go here and these

conditional rules are great there's so

many things you can do with conditional

rules it's just as creative as you can

get

what we're going to say

is

only run what we're going to do here

when

we're going to have this rule

for the static random look at itself

and we're going to say we only want it

to run

when it is blank

so that means the first time

and we want to set the field the value

to a field and that field value will be

the random id

so if it's blank meaning it's the when

the record is being created

um

it will run because it's blank it will

set the id

afterwards since it's not no longer

blank it won't run and it will retain

what it originally captured

now let's test that out here

we'll go here

now notice it's blank this is an act

behavior when you set up this type of

thing it takes a while for it to do its

thing

um

i'm just gonna

force it this is this little trick i do

with updating a lot of people do it if

we just change we make it run

update everything and then it sort of

engages stuff but let's just do this

here

now remember 866

um

update

i'll do this update it doesn't matter

we're just performing an action

so static is blank

so it's going to run

but

we had 866 it updated itself again

so now it's this now

if we do something make this a two

it changed its number here because it

will change the random number every time

it's updated but the static remains the

same that's what we want

and notice this too

this is

all these sort of

uh

[Music]

formulas or updating things or whatever

a lot of them have an order of operation

we sense

we want this after

random id

because if it was before

it would run

it would look at the

the old value it would run attract

capture that

uh and then it would regenerate the

number so just logically we want this

the static id to grab its value after

this value is already set so don't put

it before that's all i got i hope that

was helpful it's one of my shorter ones

thanks

5 views0 comments