Random Numbers Between 1 and 50; 50 Times

random numbers between 1 and 50; 50 times

You need to concatenate the innerHTML property in order to update the display. You also need to move your random number generation into your loop. Here is a sample:

function add() {  for (var i = 0; i < 49; i++) {   var a = Math.floor(Math.random() * 50) + 1;    document.getElementById("tt4").innerHTML += a + ('\n');  }}
<!DOCTYPE html><html>
<head> <title></title> <link rel="stylesheet" type="text/css" href="style.css" media="screen" /></head>
<body> <button onclick="add()">OK</button> <br><br> <textarea id="tt4" name="t4"></textarea></body>
</html>

PHP rand() ...get true 50/50 results?

Depending on the specific context in which you will use this number (e.g. a per-user basis, lifetime of application, etc.) you can store it in a number of locations, $_SESSION, a database value, or if the scope only covers the current page then you can save it on directly in that page's code.

An easy way to toggle the value is:

$val = 1 - $val;

For a database call:

UPDATE YourTable SET `next_value` = 1 - `next_value`

etc...

Generating random whole numbers in JavaScript in a specific range

There are some examples on the Mozilla Developer Network page:

/**
* Returns a random number between min (inclusive) and max (exclusive)
*/
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}

/**
* Returns a random integer between min (inclusive) and max (inclusive).
* The value is no lower than min (or the next integer greater than min
* if min isn't an integer) and no greater than max (or the next integer
* lower than max if max isn't an integer).
* Using Math.round() will give you a non-uniform distribution!
*/
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}

Here's the logic behind it. It's a simple rule of three:

Math.random() returns a Number between 0 (inclusive) and 1 (exclusive). So we have an interval like this:

[0 .................................... 1)

Now, we'd like a number between min (inclusive) and max (exclusive):

[0 .................................... 1)
[min .................................. max)

We can use the Math.random to get the correspondent in the [min, max) interval. But, first we should factor a little bit the problem by subtracting min from the second interval:

[0 .................................... 1)
[min - min ............................ max - min)

This gives:

[0 .................................... 1)
[0 .................................... max - min)

We may now apply Math.random and then calculate the correspondent. Let's choose a random number:

                Math.random()
|
[0 .................................... 1)
[0 .................................... max - min)
|
x (what we need)

So, in order to find x, we would do:

x = Math.random() * (max - min);

Don't forget to add min back, so that we get a number in the [min, max) interval:

x = Math.random() * (max - min) + min;

That was the first function from MDN. The second one, returns an integer between min and max, both inclusive.

Now for getting integers, you could use round, ceil or floor.

You could use Math.round(Math.random() * (max - min)) + min, this however gives a non-even distribution. Both, min and max only have approximately half the chance to roll:

min...min+0.5...min+1...min+1.5   ...    max-0.5....max
└───┬───┘└────────┬───────┘└───── ... ─────┘└───┬──┘ ← Math.round()
min min+1 max

With max excluded from the interval, it has an even less chance to roll than min.

With Math.floor(Math.random() * (max - min +1)) + min you have a perfectly even distribution.

min.... min+1... min+2 ... max-1... max.... max+1 (is excluded from interval)
| | | | | |
└───┬───┘└───┬───┘└─── ... ┘└───┬───┘└───┬───┘ ← Math.floor()
min min+1 max-1 max

You can't use ceil() and -1 in that equation because max now had a slightly less chance to roll, but you can roll the (unwanted) min-1 result too.

Getting a number to show up a certain percent of a time

I've written a blog post and example code for generating a random unsigned int in BGScript here: http://www.sureshjoshi.com/embedded/bgscript-random-number-generator/

Essentially, it uses a xorshift seeded by the module's serial number and/or the ADCs LSB noise to generate a pseudo-random number.

# Perform a xorshift (https://en.wikipedia.org/wiki/Xorshift) to generate a pseudo-random number
export procedure rand()
t = x ^ (x << 11)
x = y
y = z
z = rand_number
rand_number = rand_number ^ (rand_number >> 19) ^ t ^ (t >> 8)
end

and initialized here:

# Get local BT address
call system_address_get()(mac_addr(0:6))
...
tmp(15:1) = (mac_addr(0:1)/$10)+ 48 + ((mac_addr(0:1)/$10)/10*7)
tmp(16:1) = (mac_addr(0:1)&$f) + 48 + ((mac_addr(0:1)&$f )/10*7)
...
# Seed the random number generator using the last digits of the serial number
seed = (tmp(15) << 8) + tmp(16)
call initialize_rand(seed)

# For some extra randomness, can seed the rand generator using the ADC results
from internal temperature
call hardware_adc_read(14, 3, 0)
end

event hardware_adc_result(input, value)
if input = 14 then
# Use ambient temperature check to augment seed
seed = seed * (value & $ff)
call initialize_rand(seed)
end if
end

The 'randomness' of the generator can be viewed in this scatterplot - no obvious trends at a glance.

enter image description here

Once you have that, you can do similar to what Rich and John recommended by setting 'if' checks to generate your distribution. Please note that this code does not offer up a min/max value to generate a random between (due to not currently having a modulo implementation in BGScript).

Pseudo-code might be:

call rand()
if rand_number <= PROBABILITY1 then
# Show number 1
end if
if rand_number > PROBABILITY1 and rand_number <= PROBABILITY2 then
# Show number 2
end if
if rand_number > PROBABILITY2 then
# Show number 3
end if


Related Topics



Leave a reply



Submit