473,395 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

generate non repeting random numbers

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 2419
acoder
16,027 Expert Mod 8TB
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 Expert 256MB
Using Math.round() instead of Math.floor() will help reduce repetition; it splits hairs better.
Apr 6 '08 #3
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 Expert 256MB
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
replacing floor with round is not working..any other ideas ,,thnks
Apr 6 '08 #6
hsriat
1,654 Expert 1GB
[---- deleted ----]
Apr 6 '08 #7
hsriat
1,654 Expert 1GB
@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 Expert Mod 8TB
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 Expert 1GB
I think,
Expand|Select|Wrap|Line Numbers
  1. Math.floor(Math.random()* 10);
will also do the same.


kind regards
Apr 6 '08 #10
acoder
16,027 Expert Mod 8TB
Not quite, that'd give you 0 - 9.

However, your solution (in post #8) for this particular case (1-9) works fine. It'd have to change if you wanted anything greater than 9.
Apr 6 '08 #11
mrhoo
428 256MB
I would get the randomized array at one go,
and then build the html string so that
only one write needs to be called.

Expand|Select|Wrap|Line Numbers
  1. function shuffleImages(){
  2.     var A= [], i= 1;
  3.     while(i<10) A.push(i++);
  4.     A.sort(function(){return Math.random()-.5})
  5.  
  6.     var str= '<table>\n<tbody>\n';
  7.     var prefix= '<td><img src="Image';
  8.     var suffix= '.bmp"></td>';
  9.     while(A.length){
  10.         str+= '<tr>'+prefix+ 
  11.         A.splice(0,3).join(suffix+prefix)+suffix+'</tr>\n';
  12.     }
  13.     return(str+'</tbody>\n</table>');
  14.     //document.write(str+'</tbody>\n</table>');
  15. }

shuffleImages()


/* returned value:
<table>
<tbody>
<tr><td><img src="Image4.bmp"></td>
<td><img src="Image8.bmp"></td>
<td><img src="Image1.bmp"></td></tr>
<tr><td><img src="Image5.bmp"></td>
<td><img src="Image2.bmp"></td>
<td><img src="Image9.bmp"></td></tr>
<tr><td><img src="Image6.bmp"></td>
<td><img src="Image7.bmp"></td>
<td><img src="Image3.bmp"></td></tr>
</tbody>
</table>
*/
Apr 6 '08 #12

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

Similar topics

2
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...
14
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...
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...
9
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...
6
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...
20
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...
9
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
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 ...
19
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
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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
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...

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.