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

Generating random numbers in reverse order???

94
I have almost completed a monster assignment on sorting algorithms (quick, insertion and selection) using c++ but I am lost on one part of the assignment. I have to generate a random list of numbers in reverse order as follows "list[i] = 25.88 - sqrt(i)" and perform all my sorting functions no this list. I have successfully run my program on numerous lists of random numbers.. I just have no clue how to get a reverse generated list of random numbers.

Any help is appreciated. Thanks.
Nov 9 '06 #1
5 5671
dav3
94
still looking for any help on this. Its got me bamboozeled.
Nov 10 '06 #2
sicarie
4,677 Expert Mod 4TB
still looking for any help on this. Its got me bamboozeled.
Are you sure you need a list of random numbers? Anything where you have a set seed value and algorithm to produce the number will not be a "random" number.

But it sounds like you need to perform the operation 25 times, with i being 25->0.
Nov 10 '06 #3
macklin01
145 100+
I'm not entirely sure what you're asking for.

Do you want a list of random numbers, but each successive number should be smaller? e.g.,


1239.295
99.482
83.218
47.671
14.23148
12.184
11.19847
8.193
8.0582
6.2

etc.?

Well, here's the simplest way I can think of to do that:

float largest_number = 10000;
float scale = largest_number;

for( int i=0; i < total_count ; i++ )
{
list[i] = scale * rand() / (float) RAND_MAX;
scale = list[i];
}

In this way, the next random number will be between 0 and the previous number.

Is this the type of thing that you're looking for? -- Paul
Nov 10 '06 #4
dav3
94
After emailing my instructor it seems I am supposed to generate the list as follows

list[0] = 25.88 - sqrt(0) = 25.88
list[1] = 25.88 - sqrt(1) = 24.88
list[2] = 25.88 - sqrt(2) = 24.4658

however I am having issues getting my program running. I get this error and am not sure what to make of it

Error 1 error C2676: binary '[' : 'orderedArrayListType<elemType>' does not define this operator or a conversion to a type acceptable to the predefined operator

The code that generates said error is this:

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include "orderedArrayListType.h"
  4. #include <time.h>
  5. #include <math.h>
  6. using namespace std;
  7.  
  8. //double sqrt(double, double);
  9.  
  10. int _tmain(int argc, _TCHAR* argv[])
  11. {
  12.     double i;
  13.  
  14.     int maxSize;
  15.  
  16.     cout << "Please enter the size of the list: 10, 100 or 1000"<<endl;
  17.     cin >> maxSize;    //User determined size of list
  18.  
  19.     cout<<"Testing selection sort!!!"<<endl;
  20.     orderedArrayListType<int>list(maxSize);     
  21.     cout<<"Size of list = "<<list.maxListSize();
  22.  
  23.     for(i = 0; i < maxSize; i++)
  24.     {
  25.         list[i] = 25.88 - sqrt(i);
  26.     }
  27.  
  28.         cout<<"The list before sorting:"<<endl;    
  29.         list.print();                                    
  30.         cout<<endl;                                        
  31.  
  32.         list.selectionSort();    
  33.  
  34.         cout<<"The list after sorting:"<<endl;    
  35.         list.print();                                    
  36.         cout<<endl;                                        
  37.  
  38.         cout << "Selection sort: Number of comparisons = "<<list.getSortComparisons()<<endl;
  39.         cout << "Selection sort: Number of swaps = "<<list.getSortSwaps()<<endl;
  40.         cout<<endl;
  41.  
  42.  
  43.     return 0;
  44. }
  45.  
The specific line where the problem is
Expand|Select|Wrap|Line Numbers
  1. for(i = 0; i < maxSize; i++)
  2.     {
  3.         list[i] = 25.88 - sqrt(i);
  4.     }
  5.  
Nov 11 '06 #5
dav3
94
found my mistake. Thank you all who gave advice:)
Nov 11 '06 #6

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

Similar topics

1
by: Intaek LIM | last post by:
generally, we use srand(time(0)) to generate random numbers. i know why we use time(0), but i can not explain how it operates. first, see example source below. ...
16
by: Leon | last post by:
I need a program that generate 5 non-duplicates random number between 1-10 as string values store in an array. Do anybody know of any good books or websites that explain how to generator random...
104
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a...
1
by: Velhari | last post by:
Hi, I am a beginner. Please tell me, For generating Random Numbers, Initially why we are going for seed method. And another question is that, I want to print unique random number how to print by...
8
by: kiranchahar | last post by:
Hey all, How do I generate random numbers with Uniform distribution Uniform(a,b) using C-programming? I want to generate uniform random numbers which have mean following Uniform(p,q) and also...
5
by: Muffin | last post by:
I hope somebody can show me why I need to have a messagebox to get "random" numbers in my example. If I comment out the message box call that is in RollAbility() the numbers generated are not...
9
by: Tuxedo | last post by:
I'd like to reorganize the third, fourth, fifth and sixth, as well as any elements thereafter in an array in random order: var a = new...
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 ...
26
by: bilgekhan | last post by:
What is the correct method for generating 2 independent random numbers? They will be compared whether they are equal. What about this method: srand(time(0)); int r1 = rand(); srand(rand());...
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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
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...

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.