6

I'm using node.js and express, and I'd like to generate a random five-figure number in app.js and return it to the client.

I'd like to do this on the server rather than the client, because I want to be certain that the number is different for every user who is currently connected.

Here's my current (broken) code from app.js:

// My first attempt - a function to generate a random number.
// But this returns the same number to every client. 
function genRandNum() {
    return Math.floor(Math.random() * 90000) + 10000;
}
// Routes
app.get('/', function(req, res){
  res.render('index', {
    title: 'Hello world',
    random_id: genRandNum() // No good - not different for each user. 
  });
});

There are actually two problems:

  1. How can I generate a number for each client?
  2. How can I be certain the number is different for every client? Do I need to create a Redis store of currently open sessions and their numbers?

Thanks for helping out a beginner :)

Shef
  • 44,808
  • 15
  • 79
  • 90
Richard
  • 31,629
  • 29
  • 108
  • 145
  • You can't be certain with `Math.random()`, that's for sure. The chances of hitting the same number are 1 in 10^18, I think, but still... `:)` – Šime Vidas Sep 28 '11 at 13:29
  • 2
    Does it need to be random? If you incrementally assign numbers to users as they come and then in your implementation of selecting a user do that randomly you can avoid at least one of your problems. – Tegra Detra Sep 28 '11 at 13:30
  • 1
    @ŠimeVidas Odds (in this instance) are 1:90000, because the OP is using 5 digit IDs. Implies this is not an application intended for large #'s of users, and not intended for the ID to be secure or unquessable. Richard, You might find this SO page of interest: http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript – broofa Sep 28 '11 at 13:38
  • 1
    Obvious question regarding persisting this: Would storing this value in a cookie work? – broofa Sep 28 '11 at 13:40
  • It might be helpful if you stated the *purpose* of this number. – Dave Newton Sep 28 '11 at 13:48
  • @JamesAndino - no, it doesn't need to be truly random. Incremental is fine, just as long as people don't end up with the same number. – Richard Sep 28 '11 at 13:51
  • salt the random number with the users IP address or a session variable of some sort; people behind the same firewall/router will have the same IP though. – JKirchartz Sep 28 '11 at 13:52
  • @Richard The next good question is does each number have to be unique across all users or active ones!? – Tegra Detra Sep 28 '11 at 19:09

2 Answers2

2

Your code works for me, with index.jade:

h1 #{random_id}

However, as written it generates a random # for each page, not just each client. Some sort of backing data store would be needed to permanently guarantee each client has a unique #, but since you're only using 5-digits (90K possible IDs), you're clearly not concerned with this being unguessable. And if you only care about this in the context of a single node process, why not just use an auto incrementing value?

var clientId = 1;
app.get('/', function(req, res){
  res.render('index', {
    title: 'Hello world',
    random_id: clientId++ 
  });
});
broofa
  • 37,461
  • 11
  • 73
  • 73
0

For unique random numbers, you can also take the help from your user .. he can enter some random key strokes and then you can modify them/add/subtract something and send at server

Chandan
  • 229
  • 1
  • 5
  • 14