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

Random problem

35
I got a problem in generating randomly. I have a set of questions that are stored in a binary file. I have made a piece of code where these questions, when called from the binary file, are generated randomly. But i don't know why, some of the questions are repeating themselves. What do i have wrong? Thanks a lot in advance. :-)

This is the code:

Expand|Select|Wrap|Line Numbers
  1. public void askQuestions(String[] questions, String[] answers) {
  2.  
  3.         int count = 0;
  4.         int point = 0;
  5.  
  6.  
  7. int[] A = new int[questions.length];
  8. int[] B = new int[A.length];
  9.  
  10. int countA = A.length;
  11. int countB = 0;
  12.  
  13. Random generator = new Random();
  14.  
  15. for (int i = 0; i < A.length; i++){
  16.     A[i] = i;
  17. }
  18. while (countA > 0){
  19.  
  20.     int pos = generator.nextInt(A.length);
  21.  
  22.     B[countB] = A[pos];
  23.     countB++;
  24.  
  25.     for (int i = pos; i < A.length - 1; i++){
  26.         A[i] = A[i + 1];
  27.     }
  28.     countA--;
  29. }
  30.  
  31.  
  32.  
  33.         for(int j = 0; j < questions.length; j++) {
  34.  
  35.            timeForMore = true;
  36.  
  37.  
  38.           int randomIndex = B[j];
  39.           String input = JOptionPane.showInputDialog(null, questions[randomIndex]); 
  40.  
  41.  
  42.           if(answers[randomIndex].equalsIgnoreCase(input))
  43.  
  44.            count++;  
  45.            point++;
  46.  
  47.         if(!timeForMore)
  48.                 break; 
  49.      }
  50.  
  51.           JOptionPane.showMessageDialog(null, "You answered " + count +
  52.                                       " out of " + questions.length +
  53.                                       " questions correctly.");
  54. }
Jan 22 '08 #1
3 1083
Stwange
126 Expert 100+
Maybe you should consider using an ArrayList here instead. The logic seems a little flawed and long-winded at times, but most of it looks like it could work. One error (I'm not saying it is the only one) I have identified is this loop:
Expand|Select|Wrap|Line Numbers
  1. while (countA > 0){
  2.     int pos = generator.nextInt(A.length);
  3.     B[countB] = A[pos];
  4.     countB++;
  5.     for (int i = pos; i < A.length - 1; i++){
  6.         A[i] = A[i + 1];
  7.     }
  8.     countA--;
  9. }
  10.  
What you are trying to do here is select an element from the array A, place it into the array B, and then remove that element from A. Technically, this usually works, but it leaves you with a bug:
You are leaving the items at the end of the array A, and just decrementing countA so that they are not referred to any more, but in the random number generator you are using A.length, not countA, so those elements can still be selected. Try replacing A.length with countA, ie:
Expand|Select|Wrap|Line Numbers
  1. int pos = generator.nextInt(countA);
and see if that fixes it for you.
Jan 23 '08 #2
Stwange
126 Expert 100+
Also maybe your loop should be:
Expand|Select|Wrap|Line Numbers
  1. while (countA >= 0) {
because A is zero-indexed (ie. A[0] is a valid element)
Jan 23 '08 #3
saytri
35
Yeah it worked. Thanks a lot. :-)
Jan 23 '08 #4

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...
4
by: AndyW | last post by:
hey folks, Just have some code as follows:- for( int i=0; i<10; i++) { Random randomNumberGenerator; randomNumberGenerator = new Random(); int num = randomNumberGenerator.nextInt(10);...
28
by: Paul Rubin | last post by:
http://www.nightsong.com/phr/python/sharandom.c This is intended to be less predicable/have fewer correlations than the default Mersenne Twister or Wichmann-Hill generators. Comments are...
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...
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...
13
by: Roy Gourgi | last post by:
Hi, How do I invoke the random number generator that was suggested by a few people. Ideally, what I would like to do is to instantiate the random no. generator with a seed value that does not...
10
by: Curt_C [MVP] | last post by:
If I use it in my page it's fine but when I put it in a Class file for calling it returns the same # for each call. Any ideas why? I'm sure it's something I'll slap myself for but the only samples...
40
by: RadiationX | last post by:
I have a problem that I really don't understand at all. In my previous post I could get started on my projects I just had a few problems with syntax errors. This problem is something that I don't...
4
by: tshad | last post by:
I am trying to set up an Image authorization where you type in the value that is in a picture to log on to our site. I found a program that is supposed to do it, but it doesn't seem to work. ...
3
by: tshad | last post by:
I have a page that I am getting a username and password as a random number (2 letters, one number and 4 more letters) I have 2 functions I call: *************************************************...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.