473,473 Members | 1,563 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

JavaScript Random Number Generator

4 New Member
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 16722
iam_clint
1,208 Recognized Expert Top Contributor
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 New Member
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 Recognized Expert Top Contributor
Try to explain your question alittle more

i'll try to convert the C+ code
Jul 17 '06 #4
iam_clint
1,208 Recognized Expert Top Contributor
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,065 Recognized Expert Moderator Expert
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 Recognized Expert Top Contributor
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 New Member
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 Recognized Expert Top Contributor
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 New Member
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

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Brandon Michael Moore | last post by:
I'm trying to test a web application using a tool written in python. I would like to be able to generate random values to put in fields. I would like to be able to generate random dates (in a...
3
by: Joe | last post by:
Hi, I have been working on some code that requires a high use of random numbers within. Mostly I either have to either: 1) flip a coin i.e. 0 or 1, or 2) generate a double between 0 and 1. I...
70
by: Ben Pfaff | last post by:
One issue that comes up fairly often around here is the poor quality of the pseudo-random number generators supplied with many C implementations. As a result, we have to recommend things like...
5
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible...
104
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a...
12
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's...
7
by: tamsun | last post by:
we need create a GUID in web page, just like: {26C2E7C8-C689-D1D5-C452-58EC5A2F2A39} Could anyone tell me how to use javascript to create it without ActiveX object?
14
by: avanti | last post by:
Hi, I need to generate random alphanumeric password strings for the users in my application using Javascript. Are there any links that will have pointers on the same? Thanks, Avanti
11
TTCEric
by: TTCEric | last post by:
This will be original. I promise. I cannot get the random number generator to work. I tried seeding with Date.Now.Milliseconds, it still results in the same values. What I have are arrays...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.