javascript: generate 2 random but distinct numbers from range
By : user2716490
Date : March 29 2020, 07:55 AM
I hope this helps you . quick question: , Here is my attempt using splice: code :
var a = [1,2,3,4,5,6,7,8,9,10];var sample = [];
sample.push(a.splice(Math.random()*a.length,1));
sample.push(a.splice(Math.random()*a.length,1));
function sample_range(range, n) {
var sample = [];
for(var i=0; i<n; i++) {
sample.push(range.splice(Math.random()*range.length,1));
}
return sample;
}
var sample = sample_range([1,2,3,4,5,6,7,8,9,10], 2);
Array.prototype.sample_range = function(n) {
var sample = [];
for(var i=0;i<n;i++) {
sample.push(this.splice(Math.random()*this.length,1));
}
return sample;
};
var sample = [1,2,3,4,5,6,7,8,9,10].sample_range(2);
|
Generate distinct smarty random numbers
By : Eslove
Date : March 29 2020, 07:55 AM
this will help I'm using smarty v2.6 and I want to generate random distinct numbers. I'm looking for an efficient, fast way to do it using already provided Smarty functions. This is my code for generating 5 random numbers (but not distinct): , if you really need to do it in smarty templates method 1 code :
{assign var="distinct_numbers" value=array_fill(1,15,'x')}
{assign var="distinct_numbers" value=array_keys($distinct_numbers)}
{assign var="x" value=shuffle($distinct_numbers)}
{* result *}
{foreach from=$distinct_numbers item="value"}
{$value} |
{/foreach}
1 | 7 | 3 | 10 | 4 | 8 | 6 | 14 | 13 | 12 | 2 | 5 | 11 | 9 | 15 |
|
Generate distinct random numbers using procedural code
By : user3793075
Date : March 29 2020, 07:55 AM
should help you out I have been trying to generate distinct random numbers using procedural code. But i am failing to do it. Please help me in this regard. thanks in advance. , 10 distincts random numbers code :
<?php
$numbers = array();
$i = 0;
while($i < 10){
$new = rand();
$known = false;
foreach($numbers as $key => $value){
if($value == $new ){
$known = true;
break;
}
}
if(!$known){
$numbers[$i]=$new;
$i++;
}
}
var_dump($numbers);
?>
|
Will this query generate distinct random numbers in Mysql?
By : Salty
Date : March 29 2020, 07:55 AM
wish help you to fix your issue The answer is not always because the result of your query will be only the distinct values of your random numbers so if you generate the same random number 2 times, that number will appear only 1 time (you will lose 1 record).
|
Python, generate pair of distinct random numbers
By : jamaksimilijan
Date : March 29 2020, 07:55 AM
Any of those help you could use random.choice: code :
import numpy as np
np.random.choice(a=np.arange(4), size=2, replace=False)
np.random.choice(a=4, size=2, replace=False)
|