473,387 Members | 1,859 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,387 software developers and data experts.

Assign unique value to array.

I have tried to find another solution that can output as the code below. It works fine, but it is a bad way to use,because we will be hard if we have more than 10 elements.
So can anyone help me how to assign the unique value to an array that has dynamic element?
Thanks and Regards,
Sopheap Panha

Expand|Select|Wrap|Line Numbers
  1. int[] array = new int[5];
  2. Random rd = new Random();
  3.  
  4. array[0] = rd.nextInt(5);
  5. while(array[1] == array[0]){
  6.    array[1] = rd.nextInt(5);
  7. }
  8. while(array[2] == array[0] || array[2] == array[1]){
  9.    array[2] = rd.nextInt(5);
  10. }
  11. while(array[3] == array[0] || array[3] == array[1] || array[3] == array[2]){
  12.    array[3] = rd.nextInt(5);
  13. }
  14. while(array[4] == array[0] || array[4] == array[1] || array[4] == array[2] || array[4] == array[3]){
  15.    array[4] = rd.nextInt(5);
  16. }       
  17.  
  18. for (int a : array) {
  19. System.out.println(a);
  20. }
  21.  
May 4 '11 #1

✓ answered by Rabbit

Use loops
Expand|Select|Wrap|Line Numbers
  1. SomeArray[LowerBound] = RandomNumber
  2.  
  3. For i = LowerBoundOfArray+1 To UpperBoundOfArray
  4.    Do
  5.       hasMatches = False
  6.       intRandom = RandomNumber
  7.  
  8.       For j = LowerBoundOfArray To i-1
  9.          If SomeArray[j] = intRandom Then
  10.             hasMatches = True
  11.          End If
  12.       Next j
  13.    Loop While hasMatches
  14.  
  15.    SomeArray[j] = intRandom
  16. Next i

1 1998
Rabbit
12,516 Expert Mod 8TB
Use loops
Expand|Select|Wrap|Line Numbers
  1. SomeArray[LowerBound] = RandomNumber
  2.  
  3. For i = LowerBoundOfArray+1 To UpperBoundOfArray
  4.    Do
  5.       hasMatches = False
  6.       intRandom = RandomNumber
  7.  
  8.       For j = LowerBoundOfArray To i-1
  9.          If SomeArray[j] = intRandom Then
  10.             hasMatches = True
  11.          End If
  12.       Next j
  13.    Loop While hasMatches
  14.  
  15.    SomeArray[j] = intRandom
  16. Next i
May 4 '11 #2

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

Similar topics

3
by: Varun | last post by:
How to assign a value to password field from dataset Thank yo Varun
2
by: dskillingstad | last post by:
I'm trying to assign a custom value to a textbox. Here's what I have. I've created a module and "default value" code for a textbox which generates a custom auto-number (yyyy-0000) when a New...
3
by: vince | last post by:
I want to assign a null value to a DateTime variable.How do i do that? Thanks Vince
9
by: Brian Tkatch | last post by:
I'm looking for a simple way to unique an array of strings. I came up with this. Does it make sense? Am i missing anything? (Testing seems to show it to work.) Public Function Unique(ByVal...
1
by: =?Utf-8?B?Sm9l?= | last post by:
how to dynamic assign two dimensions array size say Dim myArray(x, y) as Integer
12
by: Kurda Yon | last post by:
I am wandering which way to assign a value to a session variable exist. Which of the following examples will work. Example #1: session_start(); session_register("ex"); $ex = 2.0; Example...
5
by: howa | last post by:
Hi, Consider a simple example, e.g. var a = { 'a': 'b', 'c': 'd' }
5
by: lianping | last post by:
Hi, I have a problem to assign a value into text box, below will be my script. Before view my script, let me hv quick explain on what is my problem: I would like to assign Java script value...
1
Plater
by: Plater | last post by:
I feel like this question has been asked before, but I couldn't find it. I think I am missing something obvious. Basically I am going to have a DataGridView with two columns. One textbox, one...
1
by: dissectcode2 | last post by:
HI - I have a 2d array of uint32's : uint32 x; and I want that array to be assigned to a certain address (which is a set of registers) : uint32 *addr = 0x12345678;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...

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.