473,796 Members | 2,702 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

generate non repeting random numbers

38 New Member
hey
i wrote this function to generete random numbers. it works but i don't wont these number to be repeated so any idea.
i'm using images to show the numbers.

<script type="text/javascript">
Expand|Select|Wrap|Line Numbers
  1. function shuffle()
  2. {
  3. var imgval;
  4.  
  5. document.write("<table border='1'>")
  6. var k=1;
  7.  
  8. for (var i=1;i<=3;i++)
  9. {
  10.     document.write("<tr>");
  11.         for(var j=0;j<3;j++)
  12.         {
  13.         imgval = Math.floor( 1 + Math.random() * 9);
  14.  
  15.         document.write("<td><img src='Image"+(imgval)+".bmp' </td>");
  16.  
  17.         }
  18.         k+=3;
  19.     document.write("</tr>");
  20. }
  21. document.write("</table>");
  22.  
  23.  
  24. }
</script>
Apr 5 '08 #1
11 2437
acoder
16,027 Recognized Expert Moderator MVP
Store the numbers in an array and then check against them when generating a new random number.

PS. please use [code] tags when posting code.
Apr 5 '08 #2
rnd me
427 Recognized Expert Contributor
Using Math.round() instead of Math.floor() will help reduce repetition; it splits hairs better.
Apr 6 '08 #3
eihabisaac
38 New Member
can u please edit my code to do so. if not i just have this code but i dont know where to put it,, help please

<script type="text/javascript">
<!--
Expand|Select|Wrap|Line Numbers
  1. int array = new(9);
  2.  
  3. for (int i=0; i<9; i++)
  4.    array[i] = i+1;
  5.  
  6. shuffle(array);
  7.  
  8. document.writeln("Here are the distinct random numbers:");
  9.  
  10. for (int i=1; i<9; i++)
  11. {
  12.    document.writeln(array[i-1]);
  13. }
  14.  
  15. function shuffle(int array)
  16. {
  17.   int k = 9;
  18.   int RandNum;
  19.   int Temp;
  20.   for (int i=1; i<9; i++)
  21.   {
  22.      RandNum = (int)Math.floor(Math.random()*9);
  23.  
  24.      Temp = array[k-1];
  25.      array[k-1] = array[RandNum];
  26.      array[RandNum] = Temp;
  27.      k--;
  28.   }
  29. }
  30.  
//-->
</script>
Apr 6 '08 #4
rnd me
427 Recognized Expert Contributor
this doesn't look like like javascript to me.

but at any rate, simply replace the word "floor" with the word "round" in your code.
Apr 6 '08 #5
eihabisaac
38 New Member
replacing floor with round is not working..any other ideas ,,thnks
Apr 6 '08 #6
hsriat
1,654 Recognized Expert Top Contributor
[---- deleted ----]
Apr 6 '08 #7
hsriat
1,654 Recognized Expert Top Contributor
@eihabisaac

Try this...

Expand|Select|Wrap|Line Numbers
  1. function shuffle()
  2. {
  3. var imgval;
  4.  
  5. //CHANGE 1
  6. var num = "123456789";
  7.  
  8. document.write("<table border='1'>")
  9. var k=1;
  10.  
  11. for (var i=1;i<=3;i++)
  12. {
  13.     document.write("<tr>");
  14.         for(var j=0;j<3;j++)
  15.         {
  16.  
  17.         //CHANGE 2
  18.         imgval = num.charAt(Math.floor(Math.random() * num.length));
  19.         num = num.replace(imgval,'');
  20.  
  21.         document.write("<td><img src='Image"+(imgval)+".bmp' </td>");
  22.  
  23.         }
  24.         k+=3;
  25.     document.write("</tr>");
  26. }
  27. document.write("</table>");
  28.  
  29.  
  30. }
A suggestion: Try to avoid the use of bmp format.

Using Math.round() instead of Math.floor() will help reduce repetition; it splits hairs better.
Well, I have a little disagreement with this.
I think Math.floor() will do it more even than Math.round()

Lets consider this mathematical example by generating randoms between 0 to 9 using each of the above functions.

Using Math.round(), Math.round(Math .random() * 9) will generate randoms between 0 and 9.
And Using Math.floor(), Math.floor(Math .random() * 10) will generate randoms between 0 and 9.

Results (all cases):
Expand|Select|Wrap|Line Numbers
  1. random()           round()    floor()
  2. between 0-1         0   1       0
  3. between 1-2         1   2       1
  4. between 2-3         2   3       2
  5. between 3-4         3   4       3
  6. between 4-5         4   5       4
  7. between 5-6         5   6       5
  8. between 6-7         6   7       6
  9. between 7-8         7   8       7
  10. between 8-9         8   9       8
  11. between 9-10         NA         9

So this shows, probability of occurrence of all the integers is equal in case of floor(), but in case of round(), 0 and 9 have half the probability of occurance than other integers.

So, I would recomend to use floor() (or ceil()) rather than round()

Regards,
Harpreet
Apr 6 '08 #8
acoder
16,027 Recognized Expert Moderator MVP
Just to add, if you want a random number between 1 and 9:
Expand|Select|Wrap|Line Numbers
  1. Math.floor(Math.random()* 9) + 1;
eihabisaac, please use [code] tags when posting code (see How to Ask a Question) - the last time you'll be told nicely.
Apr 6 '08 #9
hsriat
1,654 Recognized Expert Top Contributor
I think,
Expand|Select|Wrap|Line Numbers
  1. Math.floor(Math.random()* 10);
will also do the same.


kind regards
Apr 6 '08 #10

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

Similar topics

2
8290
by: Laphan | last post by:
Hi All This is a strange request, but I just cannot fathom how to do it. In theory the requirement is very basic, but in practise its a noodle!! I have 10 team names like so: Team A Team B
14
9910
by: Anthony Liu | last post by:
I am at my wit's end. I want to generate a certain number of random numbers. This is easy, I can repeatedly do uniform(0, 1) for example. But, I want the random numbers just generated sum up to 1 . I am not sure how to do this. Any idea? Thanks.
12
5231
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 RAND(). any ideas? I suppose I could use the current rancom number as the seed for the next random number. but would that really work?
9
9957
by: MyInfoStation | last post by:
Hi all, I am a newbie to Python and would like to genereate some numbers according to geometric distribution. However, the Python Random package seems do not have implemented functionality. I am wondering is there exist any other libraries that can do this job? Thanks a lot, Da
6
2784
by: Anamika | last post by:
I am doing a project where I want to generate random numbers for say n people.... These numbers should be unique for n people. Two people should not have same numbers.... Also the numbers should not be repeted.. Do anyone have some nice algorithm for that? Or anyone can suggest me any type of book or site for that purpose?
20
7861
by: jjmillertime | last post by:
I'm new so i apologize if this is in the wrong spot. I'm also new to programming in C and i've been searching for quite a while on how to create a program using C that will generate two random numbers, multiply them, and ask you for the result. It also needs to have four responses for both right and wrong answers and should print them randomly as well. The program should use at least 2 functions. Any help would be greatly appreciated. ...
9
6609
by: Chelong | last post by:
Hi All I am using the srand function generate random numbers.Here is the problem. for example: #include<iostream> #include <time.h> int main() {
24
7236
by: pereges | last post by:
I need to generate two uniform random numbers between 0 and 1 in C ? How to do it ? I looked into rand function where you need to #define RAND_MAX as 1 but will this rand function give me uniformly distributed and unique numbers ?
19
13681
by: Sanchit | last post by:
I want to generate a randowm number between two numbers x and y i.e int randomNoBetween(int x, int y); Plz help Is there any such function??
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9524
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10449
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10217
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10168
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9047
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4114
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 we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.