Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Shortest Code For Random Number Between 1 And N In Javascript
Here I want to show you a tip for creating a random number between 1 and N in easiest and shortest way.

Here is the code:

~~(Math.random () * N ) + 1

Code in textarea:

All you need to do is to replace N with your upper number.

In JavaScript we have Math.random () which creates a random number between 0 and 1, e.g. 0.3577353348955512.

We use this function to have a random number, then we multiply that to our upper number (e.g. 6):

0.3577353348955512 * 6 =

And then we get the floor number with double ~ operator
It takes so long to learn simplicity.
make The Code In Textarea Sir pls
I am a proud member of NIgerian Coders Team
(01-03-2017, 10:01 AM)naijaboy Wrote:
make The Code In Textarea Sir pls


***The code is just a one line code, dont know why you still need it in textarea***
I am a proud member of NIgerian Coders Team

Possibly Related Threads...
Thread Author Replies Views Last Post
  Can I Create Mobile App On Javascript? mogulkahn 2 10 10-11-2018, 05:32 PM
Last Post: Deelino
  ALERT:  Best Way To Convert Number To String In Javascript jonath45 7 142 08-30-2018, 09:06 AM
Last Post: Cavdy
  HELP: Come And Help Me With This Code Dilfgem 17 457 07-03-2018, 08:58 PM
Last Post: Dilfgem
  ALERT:  Ultimate Javascript Challenge jonath45 19 406 06-29-2018, 07:01 PM
Last Post: jonath45
  HELP: Pls What Are The Major Uses Of Javascript Broomboss 18 499 03-17-2018, 10:17 PM
Last Post: Broomboss
  TUTORIAL: Take A Selfie With Javascript SamBlingxZ 1 211 01-23-2018, 08:46 PM
Last Post: Broomboss
  SHARED: Introducing Hbot.js Javascript Ai Library devHammed 3 289 11-08-2017, 10:13 AM
Last Post: devHammed
  SHARED: Introducing Hrouter.js Javascript Routing Library devHammed 7 227 10-16-2017, 10:13 PM
Last Post: iamdannyfc
  SHARED: Introducing Hkwest.js Javascript Ajax Library devHammed 0 126 10-15-2017, 09:46 AM
Last Post: devHammed
  HELP: Javascript Vs Jquery. beme 10 489 08-21-2017, 07:20 AM
Last Post: beme

Users browsing this thread: 1 Guest(s)

About Nigeria's Creative Talents

Nigeria's Creative Talents is an online community where every creative Nigerians can share ideas with themselves.

For any more information, please use our contact form.

              User Links