473,804 Members | 3,478 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
11 2439
acoder
16,027 Recognized Expert Moderator MVP
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 Contributor
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
8291
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
9913
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
9959
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
2785
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
7863
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
6610
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
7238
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
13682
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
9589
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
10593
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
10340
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...
0
9163
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...
1
7626
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6858
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();...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4304
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
3830
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.