?
Solved

help with a random number

Posted on 2005-04-27
6
Medium Priority
?
156 Views
Last Modified: 2010-04-03
Here is the code i am using to create a 2 digit random number. However at the moment it produces a number from 01 - 99 but i want it to produce a numer from 01 - 49. I dont want all the code changed. I was thinking of adding a loop but think i need some help.

Rand0="";
i = 1;
while (Number(i)<>3) {
     rnd = random(10);
     x = 1;
       while (Number(x)<>Number((Number(length(Rand0))+1))) {
          if ((substring(Rand0, x, 1)) eq rnd) {
               rnd = "";
          }
          x = Number(x)+1;
     }
     if (rnd ne "") {
          Rand0 = Rand0 add rnd;
          i = Number(i)+1;
     }
}
0
Comment
Question by:lukegriffiths110
  • 3
  • 2
6 Comments
 
LVL 39

Expert Comment

by:blue-genie
ID: 13875003
hi there, seems to me a very long winded way of generating a random number,

but if i understand you correctly, and since you don't want to change your code

changing this line

      rnd = random(5);//from 10
should give you want you want.

blu
0
 
LVL 18

Accepted Solution

by:
Billystyx earned 2000 total points
ID: 13875208
but if you do want to change your code...
function rand(){
md=random(50);
}
if(md==0){
rand();
}

that way if you get a zero it runs the random no generator again until you get above 0

Billystyx
0
 
LVL 18

Expert Comment

by:Billystyx
ID: 13875362
function rand(){
md=random(50);
}
if(md==0){
rand();
}
if(md.length<2){
md="0"+md;
}
newmd=Number(md);

Billystyx
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:Billystyx
ID: 13875372
sorry, 1 change ...
rand();
function rand(){
md=random(50);
}
if(md==0){
rand();
}
if(md.length<2){
md="0"+md;
}
newmd=Number(md);
0
 

Expert Comment

by:mateuszkrzeszowiec
ID: 13875511
numBear = Math.floor(Math.random()*48 + 1);
if(numBear < 10)
numBear = "0" + numBear;
+1 is becouse you dont want to have 00
0
 

Expert Comment

by:mateuszkrzeszowiec
ID: 13875542
Sorry, mistake :D
You can check results with code below:
for(i=0;i<1000;i++) {
     numBear = Math.floor(Math.random()*49 + 1);
     if(numBear < 10)
          numBear = "0" + numBear;
     trace(numBear);
}
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes you know that one object has a specific child in it, but you can't find the child. This happened to me when I was trying to code some actionScript to make a toolbar work with its embedded buttons.  My partner had created the toolbar usi…
There are times in your Flash CS4 application when you want more than a simple pointer or a hand, and it's hard to find an ideal walk-through to tell you what to do.  I spent a few days recently learning my way around making custom cursors in Flash,…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.
Suggested Courses
Course of the Month16 days, 8 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question