0

I want to take a single digit number input by the user and generate another single digit random number based on it. A real world equivalent would be a roulette or slot machine that starts at a specific value and then randomizes.

This will generate a random number from 1-10, but does not involve any existing number as input.

var randomnumber=Math.floor(Math.random()*10) 

Is this possible?

  • I'm not really clear on what you're asking for. Is the number the user is inputting a max value of the random? If it is not... how are you determining the min/max value of what your random function should return? – Kritner Jul 30 '14 at 12:28
  • I don't think I understand your problem. Why doesn't your solution work? How will the random number be related to the input? – gion_13 Jul 30 '14 at 12:29
  • if the user enters the starting value of the random number then its: `var randomnumber= (Math.floor(Math.random()*10) ) + user_input` – hahaha Jul 30 '14 at 12:29
  • How will the random number be based on the original? – DNKROZ Jul 30 '14 at 12:30
  • he is asking for a seeded RNG – Ferdi265 Jul 30 '14 at 12:30

3 Answers3

0

Try the below code

Math.floor(Math.random() * 10) + 1

Also you can refer and use the following code based on your need

function randomNumberInterval(min,max)
{
    return Math.floor(Math.random() * (max - min + 1) + min);
}
Earth
  • 3,477
  • 6
  • 37
  • 78
0

if the user enters the starting value of the random number then its:

var randomnumber= (Math.floor(Math.random()*10) ) + user_input

example user inputs 5 and math.floor(random number) is 3, then your actual random number will be 5+3=8

hahaha
  • 1,001
  • 1
  • 16
  • 32
0

What I believe you're looking for is a seeded random number generator, the seed being the pre-existing number. This isn't possible directly with Math.Random as it just uses the current time as a seed. Take a look here for workarounds though.

Seeding the random number generator in Javascript

Seedable JavaScript random number generator

both point to this link: http://davidbau.com/archives/2010/01/30/random_seeds_coded_hints_and_quintillions.html

where a seeded random number generated has been created and is downloadable here https://github.com/davidbau/seedrandom

or for a simpler solution the second answer in the first link:

var seed = 1;
function random() {
var x = Math.sin(seed++) * 10000;
return x - Math.floor(x);
}

credit to @Antti Sykäri for that code

Community
  • 1
  • 1
DasBeasto
  • 2,082
  • 5
  • 25
  • 65