Copy link to clipboard
Copied
I'm trying to make a 6 digit random number generator between 1 and 48. When the user click a button 6 random numbers show up in 6 different textFeilds. The problem is that I don't want the same number twice. How can I generate 6 different random numbers without using return and breaking out of the function?
import flash.events.Event;
stop();
btn.addEventListener (MouseEvent.CLICK, random1);
function random1 (evt:Event) {
display1.text = "";
display2.text = "";
display3.text = "";
display4.text = "";
display5.text = "";
display6.text = "";
var r1 = Math.floor(Math.random()*(1+48-1))+1;
var r2 = Math.floor(Math.random()*(1+48-1))+1;
var r3 = Math.floor(Math.random()*(1+48-1))+1;
var r4 = Math.floor(Math.random()*(1+48-1))+1;
var r5 = Math.floor(Math.random()*(1+48-1))+1;
var r6 = Math.floor(Math.random()*(1+48-1))+1;
if (r2 == r1 || r3 == r2 || r4 == r3 || r5 == r4 || r6 == r5) {
return;
}
var liste:Array = new Array();
liste.push(r1,r2,r3,r4,r5,r6);
liste.sort(Array.NUMERIC);
display1.text = String(liste[0]);
display2.text = String(liste[1]);
display3.text = String(liste[2]);
display4.text = String(liste[3]);
display5.text = String(liste[4]);
display6.text = String(liste[5]);
}
1 Correct answer
Here's the code for the approach mentioned with your textfields included
btn.addEventListener (MouseEvent.CLICK, random1);
function random1 (evt:Event) {
var nums:Array = new Array();
// fill the array of numbers
for(var i:uint=1; i<=48; i++){
nums.push(i);
}
shuffle(nums);
// grab the first six in the shuffled array
var liste:Array = nums.splice(0,6);
// assign the values to the textfields
for(var j:uint=1; j<=6; j++){
this["display"+String(j)].text = Stri
Copy link to clipboard
Copied
One way would be to create an array containing the numbers (do it with code to save typing), shuffle the array, and then pick the first 6 numbers out of it.
Copy link to clipboard
Copied
Here's the code for the approach mentioned with your textfields included
btn.addEventListener (MouseEvent.CLICK, random1);
function random1 (evt:Event) {
var nums:Array = new Array();
// fill the array of numbers
for(var i:uint=1; i<=48; i++){
nums.push(i);
}
shuffle(nums);
// grab the first six in the shuffled array
var liste:Array = nums.splice(0,6);
// assign the values to the textfields
for(var j:uint=1; j<=6; j++){
this["display"+String(j)].text = String(liste
}
}
function shuffle(a:Array) {
var p:int;
var t:*;
var ivar:int;
for (ivar = a.length-1; ivar>=0; ivar--) {
p=Math.floor((ivar+1)*Math.random());
t = a[ivar];
a[ivar] = a
;
a
= t;
}
}
Copy link to clipboard
Copied
Here's a simpler way to get six unique numbers:
var liste:Array = new Array();
var i:int = 0;
var r1:Number;
while (i<6)
{
r1 = Math.floor(Math.random() * 48) + 1;
if (i>0)
{
if (liste.indexOf(r1) == -1)
{
i++;
liste.push(r1);
}
}
else
{
liste.push(r1);
i++;
}
}
Copy link to clipboard
Copied
here the simplest way for 6 random non repeating numbers from 1 to 20 you need more u can add it into array
var arr:Array=new Array("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20");
function randomize(a : * ,b : *) : int {
return(Math.random() > .5) ? 1 : -1;
}
arr.sort(randomize);
txt1.text=arr[0];
txt2.text=arr[1];
txt3.text=arr[2];
txt4.text=arr[3];
txt5.text=arr[4];
txt6.text=arr[5];

