|
|
|
|
![]() ![]() |
Jul 1 2006, 09:49 PM
Post
#1
|
|
|
Privileged Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 629 Joined: 26-February 05 Member No.: 3,995 |
I'm making an RPG so I need to use a lot of random numbers. Right now I'm thinking about stat increases after level-ups. I want to write a method that does something like "return a random number from 0 to 4 inclusive, but make it most likely to be 1 or 2." Does anybody know how I could go about implementing something like that?
|
|
|
|
Jul 1 2006, 10:36 PM
Post
#2
|
|
|
Trap Grand Marshal Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 1,283 Joined: 11-January 06 From: Chennai, India Member No.: 16,932 |
I cannot understand what you need. Pleas kindly explain it.
|
|
|
|
Jul 1 2006, 10:44 PM
Post
#3
|
|
|
Privileged Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 629 Joined: 26-February 05 Member No.: 3,995 |
I thought I did. I want something that will return a random number in a certain range that can be any number in that range but will usually be in a smaller range. For example, using the numbers above (0-5 inclusive but most likely 1 or 2), ten numbers returned might be:
2, 1, 1, 3, 2, 0, 1, 5, 2, 1 Seven of those are 1 or 2, and the other three are other numbers in the 0-5 range. |
|
|
|
Jul 2 2006, 10:10 AM
Post
#4
|
|
|
apt-get moo ![]() Group: [MODERATOR] Posts: 1,920 Joined: 28-May 05 From: Hertfordshire, England Member No.: 7,593 ![]() |
Well one choice would be to generate a random number from say, 1 to 100. Then if you want a 75% chance of it being 1 or 2 then say that if the first number is less than 75, generate a random number from 1 to 2, otherwise, generate one from 3 to 4. Its probably not the most efficient way of doing it, but it should work.
|
|
|
|
Jul 12 2006, 04:09 PM
Post
#5
|
|
|
Newbie [Level 2] ![]() ![]() Group: Members Posts: 33 Joined: 9-July 06 From: Belgium Member No.: 26,367 |
It's not that hard to make that! I'll explain it to you using a simple code:
CODE var numarray = new Array(1, 1, 1, 2, 2, 2, 3, 4); /* This is the array of possible values. The more times you use a number, the more times it will return. */ var rand = Math.round(Math.random()*(numarray.length-1)); /* This code creates a random number between 0 and (numarray.length-1). */ var num = numarray[rand]; /* This piece will take a value out of the array, based on the random number of the second line of code. */ And this will always return one of the values you want! Of course, you speak of a function that returns a value, so it will be more like this: CODE function randomNumber(){ var numarray = new Array(1, 1, 1, 2, 2, 2, 3, 4); var rand = Math.round(Math.random()*(numarray.length-1)); var num = numarray[rand]; return num; } This post has been edited by peroim: Jul 12 2006, 04:11 PM |
|
|
|
Jul 16 2006, 12:25 AM
Post
#6
|
|
|
Privileged Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 629 Joined: 26-February 05 Member No.: 3,995 |
Yeah that occurred to me a while after I made this topic, but it's just not as cool as it could be.
|
|
|
|
Jun 6 2007, 09:34 PM
Post
#7
|
|
|
Newbie [Level 2] ![]() ![]() Group: Members Posts: 25 Joined: 31-May 07 Member No.: 43,978 |
-
This post has been edited by augyd: Aug 21 2007, 06:20 PM |
|
|
|
![]() ![]() |
Similar Topics
| Topics | Topics | |
|---|---|---|
|
|
|
|
Lo-Fi Version | Time is now: 16th May 2008 - 02:56 PM |