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

random number help

im trying to create a list of 20 random numbers that dont repeat the same number. the first loop is good but im having trouble with the imbedded statement it looks something like this

for (int index =0; index < arraysize; index++)
{
prizeNumbers[index] = Math.abs(ran.nextInt() %500 + 1);

while ( prizeNumber[index] != prizeNumber[0] threw prizeNumber[index-1])
{
}

}
the underlined is incorrect but thats the idea im going for can i get some help with this?
Jan 20 '07 #1
6 1855
Ganon11
3,652 Expert 2GB
What language are you using? This thread would be more appropriate in a different forum.
Jan 20 '07 #2
oh java i thought i was posting in java tech section can i get a link im new to this forum havingt trouble finding things
Jan 20 '07 #3
Ganon11
3,652 Expert 2GB
The Java forum is here. I'll move the thread there now.
Jan 20 '07 #4
r035198x
13,262 8TB
im trying to create a list of 20 random numbers that dont repeat the same number. the first loop is good but im having trouble with the imbedded statement it looks something like this

for (int index =0; index < arraysize; index++)
{
prizeNumbers[index] = Math.abs(ran.nextInt() %500 + 1);

while ( prizeNumber[index] != prizeNumber[0] threw prizeNumber[index-1])
{
}

}
the underlined is incorrect but thats the idea im going for can i get some help with this?
Is there any restriction on the numbers generated? Can they be just any integers?
Jan 20 '07 #5
r035198x
13,262 8TB
Alright then. This will produce numbers below 500 but I'm sure you know how to change it to another limit

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. public class Rand {
  3.     public static void main(String... args) {
  4.         final int MAX_NUMBER = 500;
  5.         int[] randomInts = new int[20];
  6.         for(int i = 0; i < randomInts.length; i++) {
  7.             int x = (int)(Math.random()*MAX_NUMBER);
  8.             while(contains(randomInts, x)) {
  9.                 x = (int)Math.random()*MAX_NUMBER;
  10.             }
  11.             randomInts[i] = x;
  12.         }
  13.  
  14.         System.out.println(Arrays.toString(randomInts));
  15.  
  16.     }
  17.     public static boolean contains(int[] array, int val) {
  18.         for(int i : array) {
  19.             if(i == val) {
  20.                 return true;
  21.             }
  22.         }
  23.         return false;
  24.     }
  25. }
Is that what you want?
Jan 20 '07 #6
Alright then. This will produce numbers below 500 but I'm sure you know how to change it to another limit

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. public class Rand {
  3.     public static void main(String... args) {
  4.         final int MAX_NUMBER = 500;
  5.         int[] randomInts = new int[20];
  6.         for(int i = 0; i < randomInts.length; i++) {
  7.             int x = (int)(Math.random()*MAX_NUMBER);
  8.             while(contains(randomInts, x)) {
  9.                 x = (int)Math.random()*MAX_NUMBER;
  10.             }
  11.             randomInts[i] = x;
  12.         }
  13.  
  14.         System.out.println(Arrays.toString(randomInts));
  15.  
  16.     }
  17.     public static boolean contains(int[] array, int val) {
  18.         for(int i : array) {
  19.             if(i == val) {
  20.                 return true;
  21.             }
  22.         }
  23.         return false;
  24.     }
  25. }
Is that what you want?

:' ) yes thank you
Jan 20 '07 #7

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

Similar topics

10
by: Nicholas Geraldi | last post by:
Im looking for a decent random number generator. Im looking to make a large number of random numbers (100 or so, if not more) in a short period of time (as fast as possible). the function i was...
10
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random...
8
by: Aaron | last post by:
I need some help writing this function the function returns a list of random non-duplicate integers in a string, 1 parameter indicating the maximum range. string randGen(int maxRange) ...
10
by: Sonoman | last post by:
Hi all: I am trying to write a simple program that simulates asking several persons their birth day and it counts how many persons are asked until two have the same birth day. The problem that I...
4
by: anita | last post by:
I had posted this question before, but did not hear from anybody. Can somebody pls help me out. I am creating a table with two fields F1, F2 and F2 has about 50,000 randomly generated alphanumeric...
10
by: Johnny Snead | last post by:
Hey guys, Need help with this random sort algorithm private void cmdQuestion_Click(object sender, System.EventArgs e) { Random rnd = new Random(); //initialize rnd to new random object...
3
by: JoelPJustice | last post by:
I am working through a VBA book by myself to help and try and improve my skills. However, the book does not give you solutions to certain problems. I have worked through this problem up until bullet...
7
by: alphaLaura | last post by:
Hi all - I'm just new to the group and I hope nobody minds me asking for some help. I currently have an assignment which deals with matrices (more specifically, Gauss-Seidel solving of...
4
by: fatimahtaher | last post by:
Hi, I am supposed to create a program that generates a random number and then asks the user to guess the number (1-100). The program tells the user if he guessed too high or too low. If he...
2
by: alishaikhji | last post by:
I am working on a program which will need several different integer and float random numbers at different stages, for example: - At one point, I need a random number (float) in the range 0.1 to 10.0...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: 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
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
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?
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
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...

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.