Click here to Skip to main content
15,889,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to set up a promotion where ppl get a unique referral number.
They can then send that referral number to friends to enter into a form(to track referrals)

I managed the random number generator but cannot make it exclusive.

What I have tried:

<!DOCTYPE html>
<html>
<body>

<p>Click to get your referral number</p>

<button onclick="myFunction()">Referal Number</button>

<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementById("demo")
    x.innerHTML = Math.floor((Math.random() * 9999) + 1);
}
</script>

</body>
</html>
Posted
Updated 24-Apr-18 2:18am

Quote:
I managed the random number generator but cannot make it exclusive.
Use the algorithm described here: Random extraction of 5 cards from a deck[^].
 
Share this answer
 
Comments
Maciej Los 24-Apr-18 5:46am    
5ed!
CPallini 24-Apr-18 5:56am    
Thank you! :-)
Seems you want to get unique random number every time you click on the button.
To able to achieve that, you have to get the list of numbers already randomized. Then you have to generate next random number, check if already exists, if not, then to "print" all numbers.

HTML
<!DOCTYPE html>
<html>
<body>

<p>Click to get your referral number</p>

<button onclick="myFunction()">Referal Number</button>

<p id="demo"></p>

<script>
var data = []; //or new Array();
function myFunction() {
    var x = document.getElementById("demo")
    var rnum = Math.floor((Math.random() * 9999) + 1);
    //here you have to check if ranodm number exists in data
    
    //finally you have to "print" it
    
}
</script>

</body>
</html>
 
Share this answer
 
Quote:
How do I create a code for a website that will give a random non repeating number from 1-9999

This is the principle of random numbers, they can be repeated.
Quote:
I want to set up a promotion where ppl get a unique referral number.
They can then send that referral number to friends to enter into a form(to track referrals)

All the interest of a referral number is to give reward to your customers that send you new ones. In order to achieve this, you need to record the referral number in database in order to retrieve the referrer.
You can use any formula that give a unique number to each customer. Since each customer know only 1 referral number, how they are build does not really matter.
ex: start at 2000 and increment of 1: 2000, 2001, 2002, 2003 ...
ex: start at 10000 and increment of 7: 10000, 10007, 10014, 10021 ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900