0

Problem

I am creating a simple react native social media app, and I need each user to have a consistent number between 0-100 that will be used to give them a consistent color in all of their posts. I don't know an easy way to create a number that always stays the same each time you open the app. What would you reccomend?

GIISE
  • 743
  • 3
  • 9
  • 24
  • Do you have a fixed username or Id associated with your user that could be used to generate the number? – Joshua R. Nov 28 '17 at 01:19
  • @Joshua Rubin no. I looked into some logins that I could use, and none of them worked. Are there any other ways to get a username from a person? – GIISE Nov 28 '17 at 01:20
  • It seems you have not thought your problem through yet, have you? What will you do when you have more than 100 users? To answer your question, React is on client side. You should store color on the server side. Or just hash anything from the users (name, id, ...) into a 0-100 number... – RaphaMex Nov 28 '17 at 01:21
  • @R.Saban no it's okay for me to have repeating colors. It's more to differentiate people in the comments section. Since the app is anonymous I don't want to show names, only colors. – GIISE Nov 28 '17 at 01:28
  • I think we can help better if you describe more about how your user identifies themselves to the app. – Joshua R. Nov 28 '17 at 01:47
  • @Joshua Rubin as of now in my app there is no idea of a user, and everyone is the same. Everything on the servers isnt tied to a user. Anybody can read and edit the database, so no login is required at the moment. Starting the app for the first time is instant, no setting up or anything. – GIISE Nov 28 '17 at 01:52
  • Take a look at https://www.npmjs.com/package/rn-device-info. You could use DeviceInfo.getUniqueID() to feed R. Saban's hash-based solution below. – Joshua R. Nov 28 '17 at 01:56
  • @Joshua Rubin thanks! I'll check it out! – GIISE Nov 28 '17 at 01:58

1 Answers1

1

I recommend you hash any info of your users whose values are well distributed (names, ids, ...). Then bring back this hash value between 0 and 100 with a simple modulo.

Read this: Generate a Hash from string in Javascript/jQuery

And just change the end by:

return hash % 101;
RaphaMex
  • 2,781
  • 1
  • 14
  • 30