468,290 Members | 1,978 Online

# JavaScript Random Number Generator 4
I like to make a javascript that generate random number from the range number within : (10016486 and 99999985).
the number always + 22423 that begin from 10016486 and end at 99999985
Example random result from generate button :

10016486 + 22423 = 10038909
10038909 + 22423 = 10061332 and so on, maximum to 99999985

or if somebody can convert below c++ code to working javascript/html random number generator.
Expand|Select|Wrap|Line Numbers
1. #include <cstdlib>
2. #include <ctime>
3. #include <iostream>
4.
5. using std::cout;
6.
7. int main( ) {
8.
9.   // srand will set a random starting point for rand()
10.   // We use the time as a seed, as it's always changing
11.   srand( ( unsigned )time( 0 ) );
12.
13.   // We generate a random number between 0 and 4013
14.   // 4013 because ( 22423 * 4013 => 89983499 and
15.   // 89983499 + 10016486 => 99999985 - our max value )
16.   int randomKey = rand() % 4013;
17.
18.   // Output the generated key and don't immediately close the command prompt window
19.   cout << "Your random key is: " << ( 10016486 + ( randomKey * 22423 ) ) << "\n";
20.   system( "PAUSE" );
21.
22.   return 0;
23. }
Thanks for any help.
Jul 16 '06 #1
9 16356 iam_clint
1,208 Expert 1GB
Expand|Select|Wrap|Line Numbers
1. <html>
2. <body>
3. <script>
4. function randomnumber() {
5.     document.getElementById("rand").value = Math.round((99977562 - 10016486) * Math.random() + 1) + 22423;
6. //alright so 99977562 because 99977562 + 22423 = 99999985 and thats the max don't want to go over.
7. //so what i did here was first 99977562 - 10016486 x math.random() + 1 + 22423
8. }
9. </script>
10. <input type="text" name="rand" id="rand">
11. <input type="button" value="insert" onclick="randomnumber();">
12. </body>
13. </html>
14.
thats what i came up with by your explanation but that doesn't look like whats the c code is doing good luck
Jul 16 '06 #2
L33VaNcL33F
4 Thanks for the script.

But the generated value is not valid.
I don't know where is the wrong.
Jul 17 '06 #3
iam_clint
1,208 Expert 1GB
Try to explain your question alittle more

i'll try to convert the C+ code
Jul 17 '06 #4
iam_clint
1,208 Expert 1GB
Expand|Select|Wrap|Line Numbers
1. <script>
2. function randomnumber() {
3.     //alrighty this is basicly doing the same as rand() % 4013 all that says is a random number between 0 and 4013
4.     randomkey = Math.floor(Math.random()*4013);
5.     //next step is do add 10016486 + (the random number * 22423) for the final value
6.     document.getElementById("rand").value = (10016486 + (randomkey * 22423));
7. }
8. </script>
9. <input type="text" name="rand" id="rand">
10. <input type="button" value="Get Random Key" onclick="randomnumber();">
11. </body>
12. </html>
13.
this is what i came up with i tried to do exactly what the C program is doing.

i think this is more along the lines of what you are looking for. let me know if everything works out
Jul 17 '06 #5
Banfa
9,033 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
1.     //alrighty this is basicly doing the same as rand() % 4013 all that says is a random number between 0 and 4013
2.     randomkey = Math.floor(Math.random()*4013);
3.
Actually

Expand|Select|Wrap|Line Numbers
1. rand() % 4013
2.
produces a number between 0 and 4012 because 4013 % 4013 = 0. On a C/C++ note this is a really bad way to produce a small random number because the normal pseudo random number generator used in most (well a lot) of C implementations doesn't have very good randomness in the lower order bits of the value

Expand|Select|Wrap|Line Numbers
1. (rand()/(MAX_RAND/4013))%4013
2.
is better although you have a slightly greater chance of getting 0 than any other number. This can be eliminated but it takes more lines of code than I care to type here since it is in an irrelevent language.
Jul 17 '06 #6
iam_clint
1,208 Expert 1GB
Thanks for the explanations i don't do much c coding but my javascript code should be producing the same results? Is it correct or did i miss something that c is doing that the javascript i made for him isn't doing? the first one i made was off his explination the second one was based off the c++ code if you have a javascript solution to his answer please post it i did what i could as to converting the C++ code.

I think it should generate the numbers in the correct range that he wanted.
Jul 17 '06 #7
L33VaNcL33F
4 Thanks for the explanations i don't do much c coding but my javascript code should be producing the same results? Is it correct or did i miss something that c is doing that the javascript i made for him isn't doing? the first one i made was off his explination the second one was based off the c++ code if you have a javascript solution to his answer please post it i did what i could as to converting the C++ code.

I think it should generate the numbers in the correct range that he wanted.
Oh really thank you iam_clint,
The second one generated the right value.

My first C++ code is too simple. but below is some additional code.
Need some help again how to add prefix like ABC in front of the generated value number, for example ABC12345678. Thanks for the script.

Expand|Select|Wrap|Line Numbers
1. #include <iostream>
2. #include <ctime>
3. #include <cstdlib>
4.
5. using namespace std;
6.
7. int main()
8.     srand((unsigned)time(0));
9.     int random_integer = rand() % 4013;
10.     int lowest=0, highest=4013;
11.     int range=(highest-lowest)+1;
12.     for(int index=0; index<20; index++){
13.         random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
14.         cout << "Your random numbers is: ABC" << ( 10016486 + ( random_integer * 22423 ) ) << "\n";
15.         system( "PAUSE" );
16.     }
17. }
Jul 20 '06 #8
iam_clint
1,208 Expert 1GB
Expand|Select|Wrap|Line Numbers
1.
2. <script>
3. function randomnumber() {
4.     randomkey = Math.floor(Math.random()*4013);
5.     document.getElementById("rand").value = "abc" + (10016486 + (randomkey * 22423));
6. }
7. </script>
8. <input type="text" name="rand" id="rand">
9. <input type="button" value="Get Random Key" onclick="randomnumber();">
10. </body>
11. </html>
12.
this would produce abc then the random number i did not change any of the javascript code to match up with your new C++ code. just added the string
Jul 20 '06 #9
L33VaNcL33F
4 Thank You very much iam_clint,
You give me an inspiration about coding.
I will study JavaScript code deeply and seriously.

With this help, I got many knowledge and clues how to coding.
Thanks again.
Jul 21 '06 #10

 1 post views Thread by Brandon Michael Moore | last post: by 3 posts views Thread by Joe | last post: by 70 posts views Thread by Ben Pfaff | last post: by 5 posts views Thread by Peteroid | last post: by 104 posts views Thread by fieldfallow | last post: by 12 posts views Thread by Jim Michaels | last post: by 7 posts views Thread by tamsun | last post: by 14 posts views Thread by avanti | last post: by 11 posts views Thread by TTCEric | last post: by reply views Thread by ravipankaj | last post: by reply views Thread by ravipankaj | last post: by reply views Thread by ravipankaj | last post: by reply views Thread by NPC403 | last post: by reply views Thread by bitcalf33 | last post: by 3 posts views Thread by MrBee | last post: by 2 posts views Thread by MrBee | last post: by reply views Thread by Swartbj | last post: by 1 post views Thread by Aftab Ahmad | last post: by